我有一些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();
有谁知道我做错了什么?
答案 0 :(得分:3)
试试这个:
Func<MyObject, bool> datePredicate = (o => o.Date > DateTime.Now.AddDays(-1));
collection.Where(datepredicate);
此外,当您正在进行.SingleOrDefault()
时,由于Type
不是List<T>
据我所知,不确定这会如何神奇地转变为List<Type>
因为Type
没有Date
属性。
答案 1 :(得分:0)
编译器无法静态地确定Type
o
参数是什么o
。我认为DateTime
的类型为Predicate<DateTime> datePredicate = o => o.Date > DateTime.Now.AddDays(-1);
。编译器不做推定:)
{{1}}
试试。