【问题标题】:Linq group join and where statement on property of the joined tableLinq 组连接和连接表属性的 where 语句
【发布时间】:2016-12-04 21:55:28
【问题描述】:

所以我相信我发现group joinleft outer join,这就是我所需要的。但我需要检查连接表属性是否为null。但我还没有开始工作。

所以我基本上需要 Linq 实体框架中的这个查询的等价物

SELECT
    id, test, test2 
FROM Table1 
LEFT OUTER JOIN Table2 ON 
    table1.id = table2.id 
WHERE table2.example = NULL;

我曾尝试使用 lambda 执行此操作,但尚未成功。我似乎无法掌握where 语句的table2 属性示例。

【问题讨论】:

标签: c# sql-server entity-framework linq join


【解决方案1】:

您可以使用 LINQ 扩展方法 (GroupJoin) 处理此示例:

    Table1.GroupJoin(Table2,
                    x => x.ID,
                    y => y.ID,
                    (tbl1, tbl2) => new {Table1=tbl1, Table2 =tbl2.DefaultIfEmpty()})
                    .SelectMany(
                    tbl => tbl.Table2.Where(t2 => t2.example == null).Select(x => new
                    {
                        id= tbl.Table1.ID,
                        test = tbl.Table1.Test,
                        test2 = tbl.Table2.Test
                    }))ToList();

【讨论】:

    【解决方案2】:

    您可能想查看:http://www.sqltolinq.com/

    Linqer 是一个 SQL 到 LINQ 的转换工具。它可以帮助您学习 LINQ 并转换您现有的 SQL 语句。

    并非每条 SQL 语句都可以转换为 LINQ,但 Linqer 涵盖了许多不同类型的 SQL 表达式。

    假设您在 EF dbcontext 中有 Table1 和 Table2。

    from Table1 in context
    from Table2 in context
        .Where(t2=> t2.ID == Table1.ID && t2.example == null).DefaultIfEmpty()
    select new
    {
         id= Table1.ID
        ,test = Table1.Test
        ,test2 = Table2.Test
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多