类型推断在调用失败“加入”使用匿名类型时 [英] Type inference failed in the call to 'Join' when using anonymous type
问题描述
我有一个特殊的问题,LINQ to SQL的:
I have a peculiar problem with LINQ to SQL:
这样做是好的:
from s in Something
join a in AnotherThing
on s.NullableDateTime.Value
equals a.DateTime
select s
然而,使用匿名类型,像这样:
However, using anonymous type like so:
from s in Something
join a in AnotherThing
on new { s.NullableDateTime.Value }
equals new { a.DateTime }
select s
在
结果
的联接子句中的表达式之一的类型不正确。类型推断在调用加入失败。
The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
我需要为我的目标是添加另一列加入到使用匿名类型
I need to use anonymous types as I aim to add another column to join on.
任何想法,这是为什么发生,以及如何解决?
Any ideas as to why this is occurring and how to fix?
推荐答案
您应该告诉编译器,什么样的属性必须比较:
You should tell to compiler, what properties it must compare:
on new { s.NullableDateTime.Value } equals new { Value = a.DateTime }
的首先创建一个匿名类型,它看起来像这样的:
The first creates an anonymous type, which looks like this:
class A { public DateTime Value { get; set; } }
您样品中的第二行创建另一个匿名类型:
The second line in your sample creates another anonymous type:
class B { public DateTime DateTime { get; set; } }
因此,编译器无法理解,你要比较
a.value中
与b.DateTime
。这篇关于类型推断在调用失败“加入”使用匿名类型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!