C# LINQ - 'join 子句中的表达式之一的类型不正确。调用“GroupJoin”时类型推断失败。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10137346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ - 'The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.'
提问by Willem
I have this query wit a group join:
我有一个带组加入的查询:
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
...
}
I would like to know if it is possible to have a group join as above?
我想知道是否可以按上述方式加入群组?
new { t1.SomeID, item.SomeName} equals new {t2.SomeID, t2.SomeName}
item.SomeNamecomes from the list i am iterating through.
item.SomeName来自我正在迭代的列表。
If not, how would i change the statement to get the desired results?
如果没有,我将如何更改语句以获得所需的结果?
采纳答案by Adrian Iftode
The types of the properties used with the equals expression must match. So for example is Table1.SomeIDis Int32and Table2.SomeIDis Nullable<Int32>, then they don't match.
与 equals 表达式一起使用的属性类型必须匹配。因此,例如是Table1.SomeID是Int32和Table2.SomeID是Nullable<Int32>,然后他们不匹配。
EDIT
编辑
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
...
}
Also check item.SomeName is same type as t2.SomeName
还要检查 item.SomeName 与 t2.SomeName 的类型相同
回答by Hà Tay Quê R??u
In this case you must be sure that the properties and type of the two new anonymous objects are the same. I usually give specific name of properties.
在这种情况下,您必须确保两个新匿名对象的属性和类型相同。我通常会给出具体的属性名称。
Ex.:
前任。:
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

