Entityframework Join using join method and lambdas

Entity FrameworkJoinLambda

Entity Framework Problem Overview


It seems there are different ways to do joins using linq. One is more straightforward and involves just joining tables like this:

var found = from c in s.categories
            join cm in s.categorymaps on c.CategoryId equals cm.ChildCategoryId
            select c;

There is another way using lambdas and I'm having a heck of a time trying to figure out how to join using this syntax. Can anyone provide links to detailed explanations and lots of examples? Or just simply demonstrate using examples how to use this rather confusing syntax?

var x = _session.All<category>().Join<categorymap,category, ....

Entity Framework Solutions


Solution 1 - Entity Framework

Generally i prefer the lambda syntax with LINQ, but Join is one example where i prefer the query syntax - purely for readability.

Nonetheless, here is the equivalent of your above query (i think, untested):

var query = db.Categories         // source
   .Join(db.CategoryMaps,         // target
      c => c.CategoryId,          // FK
      cm => cm.ChildCategoryId,   // PK
      (c, cm) => new { Category = c, CategoryMaps = cm }) // project result
   .Select(x => x.Category);  // select result

You might have to fiddle with the projection depending on what you want to return, but that's the jist of it.

Solution 2 - Entity Framework

You can find a few examples here:

> // Fill the DataSet. > DataSet ds = new DataSet(); > ds.Locale = CultureInfo.InvariantCulture; > FillDataSet(ds); >
> DataTable contacts = ds.Tables["Contact"]; > DataTable orders = ds.Tables["SalesOrderHeader"]; >
> var query = > contacts.AsEnumerable().Join(orders.AsEnumerable(), > order => order.Field("ContactID"), > contact => contact.Field("ContactID"), > (contact, order) => new > { > ContactID = contact.Field("ContactID"), > SalesOrderID = order.Field("SalesOrderID"), > FirstName = contact.Field("FirstName"), > Lastname = contact.Field("Lastname"), > TotalDue = order.Field("TotalDue") > }); >
>
> foreach (var contact_order in query) > { > Console.WriteLine("ContactID: {0} " > + "SalesOrderID: {1} " > + "FirstName: {2} " > + "Lastname: {3} " > + "TotalDue: {4}", > contact_order.ContactID, > contact_order.SalesOrderID, > contact_order.FirstName, > contact_order.Lastname, > contact_order.TotalDue); > }

Or just google for 'linq join method syntax'.

Solution 3 - Entity Framework

If you have configured navigation property 1-n I would recommend you to use:

var query = db.Categories                                  // source
   .SelectMany(c=>c.CategoryMaps,                          // join
      (c, cm) => new { Category = c, CategoryMaps = cm })  // project result
   .Select(x => x.Category);                               // select result

Much more clearer to me and looks better with multiple nested joins.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMasonView Question on Stackoverflow
Solution 1 - Entity FrameworkRPM1984View Answer on Stackoverflow
Solution 2 - Entity FrameworkJakub KoneckiView Answer on Stackoverflow
Solution 3 - Entity FrameworkGetoXView Answer on Stackoverflow