Book Image

WCF Multi-layer Services Development with Entity Framework - Fourth Edition

By : Mike Liu
Book Image

WCF Multi-layer Services Development with Entity Framework - Fourth Edition

By: Mike Liu

Overview of this book

Table of Contents (20 chapters)
WCF Multi-layer Services Development with Entity Framework Fourth Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Hosting the HelloWorld WCF Service
Index

Joining two tables


Although an association is a kind of join in LINQ, we can also explicitly join two tables using the Join keyword, as shown in the following code snippet:

    static void TestJoin()
    {
        using(var NWEntities = new NorthwindEntities())
       {
        var categoryProducts =
            from c in NWEntities.Categories
            join p in NWEntities.Products 
            on c.CategoryID equals p.CategoryID 
            into productsByCategory
            select new {
                c.CategoryName,
                productCount = productsByCategory.Count()
            };
            
        foreach (var cp in categoryProducts)
        {
          Console.WriteLine("There are {0} products in category {1}",
                cp.productCount, cp.CategoryName);
        }
       }
    }

This was not so useful in the previous example because the Products and Categories tables are associated with a foreign key relationship. If there is no foreign key association between two...