C# 具有lambda表达式的多个左外联接

C# 具有lambda表达式的多个左外联接,c#,sql,sql-server,entity-framework,lambda,C#,Sql,Sql Server,Entity Framework,Lambda,我有一个SQL查询来处理像这样的Lambda表达式,通常比本例中有更多的连接 select Table2.a, Table2.b, Table2.c, Table2.d from Table1 LEFT OUTER JOIN Table2 ON Table2.a = Table1.a and Table2.b = Table1.b and Table2.c = Table1.c

我有一个SQL查询来处理像这样的Lambda表达式,通常比本例中有更多的连接

select Table2.a,
          Table2.b,
          Table2.c,
          Table2.d
   from Table1
     LEFT OUTER JOIN Table2
     ON Table2.a = Table1.a and
        Table2.b = Table1.b and
        Table2.c = Table1.c 
     LEFT OUTER JOIN Table3
     ON Table3.b = Table1.b AND
        Table3.c = Table1.c AND
        Table3.d = Table1.d 
   where ( Table1.a = ValueA )
   order by Table3.f
我使用Join()Lambda表达式来执行此操作,但我在SQL Server profiler中看到,这将生成一个内部联接,而我需要一个左外部联接

这就是我使用Join()的方式


我一直在读到可以使用DefaultIfEmpty()或GroupJoin()来完成,但我没有发现任何包含多个左外连接的复杂示例。

为什么不尝试使用linq查询,与lambda表达式相比,这两种方法都更易于编写和理解。我对此类实施有如下建议:

var products = 
        from p in this.Products
        from cat in this.ProductCategoryProducts
        .Where(c => c.ProductID == p.ProductID).DefaultIfEmpty()

        from pc in this.ProductCategories 
        .Where(pc => ac.ProductCategoryID == cat.ProductCategoryID).DefaultIfEmpty()

        where p.ProductID == productID
        select new
        {
            ProductID = p.ProductID,
            Heading = p.Heading,                
            Category = pc.ProductCategory
        };
    return products ;

你能显示你有哪些导航属性吗?当您可以使用类似于
orderby table1.Table3.f
的语法时,这就容易多了。您好,我的lambda表达式没有指明它,因为我希望在lambda表达式末尾选择的原因是实体的所有字段。因为您知道如何使用左连接编写查询,为什么不直接调用它或者把它放在一个存储的进程中并调用它呢?
var products = 
        from p in this.Products
        from cat in this.ProductCategoryProducts
        .Where(c => c.ProductID == p.ProductID).DefaultIfEmpty()

        from pc in this.ProductCategories 
        .Where(pc => ac.ProductCategoryID == cat.ProductCategoryID).DefaultIfEmpty()

        where p.ProductID == productID
        select new
        {
            ProductID = p.ProductID,
            Heading = p.Heading,                
            Category = pc.ProductCategory
        };
    return products ;