13

我有这个查询与组加入:

    foreach (var item in someList)
    {
                    var result = (from t1 in someContext.Table1
                                  join t2 in someContext.Table2 on new { t1.SomeID, item.SomeName} equals new {t2.SomeID, t2.SomeName} into j1 
                                  ...
    }

我想知道是否可以像上面那样加入一个小组?

new { t1.SomeID, item.SomeName} equals new {t2.SomeID, t2.SomeName}

item.SomeName来自我正在迭代的列表。

如果没有,我将如何更改语句以获得所需的结果?

4

2 回答 2

30

与 equals 表达式一起使用的属性类型必须匹配。例如 is Table1.SomeIDisInt32Table2.SomeIDis Nullable<Int32>,那么它们不匹配。

编辑

foreach (var item in someList)
    {
       var someName = item.SomeName;
       var result = (from t1 in someContext.Table1
                     join t2 in someContext.Table2 on 
                               new { t1.SomeID, SomeName = someName} 
                        equals new { t2.SomeID, t2.SomeName} into j1 
                                  ...
    }

还要检查 item.SomeName 与 t2.SomeName 的类型相同

于 2012-04-13T08:14:45.363 回答
16

在这种情况下,您必须确保两个新匿名对象的属性和类型相同。我通常给出属性的具体名称。

前任。:

 var result = from t1 in someContext.Table1
              join t2 in someContext.Table2 on 
                          new { SomeID = t1.SomeID, SomeName = someName} equals 
                          new { SomeID = t2.SomeID, SomeName = t2.SomeName} into j1 
于 2013-04-12T02:32:46.567 回答