【问题标题】:linq to sql join on multiple columns using lambda errorlinq to sql 使用 lambda 错误连接多个列
【发布时间】:2021-02-04 14:05:15
【问题描述】:

我尝试按照以下示例翻译此查询: linq to sql join on multiple columns using lambda

from tabA in this.context.TabA
join tabB in this.context.TabB on
new
{
   field1 = tabA.Soc,
   field2 = tabA.ID
}
equals new
{
   field1 = TabB.Soc,
   field2 = TabB.Code
}
select tabA;

用这个:

var query= this.context.TabA.Join(
    this.context.TabB,
    s => new { s.Soc, s.ID },
    h => new { h.Soc, h.Code },
    (s, h) => s);

但是 Visual Studio Intellisense 报告了这个我不理解的错误(加入红色信号给我):

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    匿名类型的名称必须匹配。在第一个查询中,您给它们命名为 field1field2。第二个查询将在第一个查询上使用Soc\ ID,在第二个查询上使用Soc\ Code,而Soc 字段将匹配ID 名称将不匹配Code 名称。因此,与您的查询语法等效的方法语法实际上是

    var query= this.context.TabA.Join(
        this.context.TabB,
        s => new { field1 = s.Soc, field2 = s.ID },
        h => new { field1 = h.Soc, field2 = h.Code },
        (s, h) => s);
    

    但您实际上只需要命名匿名类的第二个属性,因为在这两种情况下它都会将名称 Soc 作为第一个属性。

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2018-10-24
      相关资源
      最近更新 更多