使用显式定义的谓词时出现 C# 错误
我有许多 lambda 表达式,它们将在 where 子句中使用相同的谓词。因此,我是第一次使用谓词类型。这就是我所拥有的..
Predicate<Type> datePredicate = o => o.Date > DateTime.Now.AddDays(-1);
当我在查询中使用它时(如下),我收到以下错误..
错误:
The type arguments for method 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,bool>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
用法:
Type t = collection.Where(datePredicate).SingleOrDefault();
有人知道我做错了什么吗?
i have a number of lambda expressions that will be using the same predicate in a where clause. As such i am using the predicate type for the first time. Here is what i have..
Predicate<Type> datePredicate = o => o.Date > DateTime.Now.AddDays(-1);
When i use it in my query (below), i am getting the following error..
Error:
The type arguments for method 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,bool>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Usage:
Type t = collection.Where(datePredicate).SingleOrDefault();
Does anyone know what i am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
另外,当您执行
.SingleOrDefault()
时,不确定它如何神奇地变成Type
因为您的List
据我所知, > 不是List
(因为Type
没有Date
属性)。Try this:
Also when you're doing
.SingleOrDefault()
, not sure how that will magically turn into aType
since yourList<T>
is not aList<Type>
as far as I know (sinceType
doesn't have aDate
property).编译器无法静态地找出您的
o
参数的Type
类型。我假设o
是DateTime
类型。编译器不会做出假设:)尝试一下。
The compiler cannot statically figure out what
Type
youro
parameter is. I would presumeo
is of typeDateTime
. Compilers don't make presumptions :)Try that.