I read all the posts related to the implementation of the LEFT OUTER JOIN equivalent in Linq to Entities (.NET 3.5) when using the Entity Framework, but have not yet found a solution to the following problem.
For the two tables below:
public class Foo { public int FooId;
I need to create a Linq to Entities statement, which is the EQUIVALENT of the next SQL statement. Note that the WHERE clause contains two OR'd clauses that span both tables, and the use of the DISTINCT qualifier.
SELECT DISTINCT Foo.* FROM Foo LEFT OUTER JOIN Bar ON Foo.FooId = Bar.FooId WHERE (Foo.Name = 'fooname' OR Bar.Desc = 'bardesc')
The Linq query that I generate is Linq for Entities through the Entity Framework and will (hopefully) generate a single SQL statement that will be executed on the server. Linq to Entities does not support the FirstOrDefault () extension clause, so the standard Linq syntax for the LEFT OUTER JOIN will not work.
Here is the solution that I have SO FAR, but I can not do one of the following:
1) Create a result set containing a set of Foo / Bar combinations that will be returned by the LEFT OUTER JOIN operation.
2) Implement the equivalent of the WHERE clause: WHERE (Foo.Name = 'fooname' OR Bar.Desc = 'bardesc')
private class JoinSet { public Foo Foo; public IQueryable<Bar> Bars; }; private class FooBar { public Foo Foo; public Bar Bar; }; IEnumerable<Foo> OuterJoinTest() { IQueryable<Foo> fooBaseQuery = dbContext.FooSet; IQueryable<Bar> barBaseQuery = dbDontext.BarSet; IQueryable<JoinSet> joinQuery = from foo in fooBaseQuery select new JoinSet { Foo = foo, Bars = barBaseQuery.Where(bar => bar.FooId == foo.FooId) };
Any help, ideas or suggestions would be appreciated.
EulerOperator
source share