Linq to entites left outer join?

Anybody who tells you to use .DefaultIfEmpty() as part of an outer join in LINQ to Entities hasn't actually tried it themselves! Tt simply does not work - at least as at . NET 3.5 SP1 This blogger tells you how you should actually do it.

Essentially, . NET does outer joins in LINQ to Entities by default so you should leave out the .DefaultIfEmpty(). For multiple outer joins you have to nest the query groups to keep their context clear.

Anybody who tells you to use .DefaultIfEmpty() as part of an outer join in LINQ to Entities hasn't actually tried it themselves! Tt simply does not work - at least as at . NET 3.5 SP1.

This blogger tells you how you should actually do it. Essentially, . NET does outer joins in LINQ to Entities by default, so you should leave out the .DefaultIfEmpty().

For multiple outer joins you have to nest the query groups to keep their context clear.

Tht link is not working – Ashes Sep 20 at 10:08.

Below is your solution to achieving a left join. In terms of other resources I really recommend trying out linq pad: linqpad.net/ It is a great learning tool for Linq. // Listing class/container/table public class Listing { public string ListingID {get;set;} public Int32?

MakeID {get;set;} } // Make class/container/table public class Make { public Int32 MakeID {get;set;} public string Description {get;set;} } public class Main { public static void LinqMain() { // Populate the listing table with data List listings = new List() { new Listing() { ListingID = "Test 1", MakeID = 1 }, new Listing() { ListingID = "Test 2", MakeID = 1 }, new Listing() { ListingID = "No Make", MakeID = null }, new Listing() { ListingID = "Test 3", MakeID = 3 }, new Listing() { ListingID = "Another Makeless", MakeID = null } }; // Populate the makes table with data List makes = new List() { new Make() { MakeID = 1, Description = "Make 1"}, new Make() { MakeID = 2, Description = "Make 2"}, new Make() { MakeID = 3, Description = "Make 3"}, new Make() { MakeID = 4, Description = "Make 4"} }; // Return the left join on Make Id var result = from l in listings // These two lines are the left join. Join leftm in makes on l. MakeID equals leftm.

MakeID into leftm from m in leftm.DefaultIfEmpty() // To ensure the select does not get bogged down with too much logic use the let syntax let description = m == null? "NA" : m. Description select new { l.

ListingID, l. MakeID, description }; } The result variable would contain: { ListingID = "Test 1", MakeID = 1, description = "Make 1" } { ListingID = "Test 2", MakeID = 1, description = "Make 1" } { ListingID = "No Make", MakeID = null, description = "NA" } { ListingID = "Test 3", MakeID = 3, description = "Make 3" } { ListingID = "Another Makeless", MakeID = null, description = "NA" }.

Seravy : Nice verbose answer :) – Pure. Krome Feb 4 '10 at 12:55 Thanks @Seravy but I got this error LINQ to Entities does not recognize the method 'System.Collections.Generic. IEnumerable`1AutoSales.AutoSalesCore.Domain.Impl.Ca?

Tegories DefaultIfEmptyCategories(System.Collections.Generic. IEnumerable`1AutoSales.Au?toSalesCore.Domain.Impl. Categories)' method, and this method cannot be translated into a store expression.

As DefaultIfEmpty() is not applicable to linq to entities. – Parminder Feb 5 '10 at 16:06.

oddiandeveloper.blogspot.com/2008/12/tes... This should help, it's a blog post I made a while ago, should still be relevant and might help with testability as well. Also make sure your foreign keys are in place when you generate your entity model, it'll help setup your dependencies.

Var x = from l in listings join m in makes on l. Makeid equals m. Makeid into g from ma in g.DefaultIfEmpty() select new { l.

ListingID, l. Makeid, (ma. Description == null?

"NA" : ma. Description) }; If you have any trouble with it, let me know and i'll check on my work pc.

Masters join q in ctx. Details on p.Id equals q. Select new {Id = p.Id,m1 = p.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions