Entityframework Join using the join method and lambdas

There seem to be different ways to connect using linq. One of them is simpler and simply involves joining tables as follows:

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

There is another way to use lambdas, and I have damn time trying to figure out how to join using this syntax. Can someone provide links to detailed explanations and many examples? Or just demonstrate, using examples, how to use this rather confusing syntax?

 var x = _session.All<category>().Join<categorymap,category, .... 
+45
join lambda entity-framework
Feb 15 '11 at 22:09
source share
2 answers

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

However, here is the equivalent of your above query (I think, unverified):

 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 may have to play with the projection depending on what you want to return, but in what its essence.

+121
Feb 15 '11 at 23:01
source share

You can find some 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<Int32>("ContactID"), contact => contact.Field<Int32>("ContactID"), (contact, order) => new { ContactID = contact.Field<Int32>("ContactID"), SalesOrderID = order.Field<Int32>("SalesOrderID"), FirstName = contact.Field<string>("FirstName"), Lastname = contact.Field<string>("Lastname"), TotalDue = order.Field<decimal>("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 the linq join syntax.

+11
Feb 15 '11 at 10:25
source share



All Articles