我有一个群组加入的查询:
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
来自我正在迭代的列表。
如果没有,我将如何更改语句以获得所需结果?
答案 0 :(得分:24)
与equals表达式一起使用的属性类型必须匹配。例如,Table1.SomeID
为Int32
而Table2.SomeID
为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
的类型相同答案 1 :(得分:11)
在这种情况下,您必须确保两个新匿名对象的属性和类型相同。我通常给出特定的属性名称。
例:
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