C# 如何在linq where子句中使用具有泛型func lambda的表达式?
这是我的C# 如何在linq where子句中使用具有泛型func lambda的表达式?,c#,linq,lambda,expression-trees,C#,Linq,Lambda,Expression Trees,这是我的 private readonly Dictionary<Type, List<object>> _cache; public IList<T> Get<T> (Expression<Func<T, bool>> criteria) { return _cache[typeof(T)].Where (criteria); } 专用只读字典\u缓存; 公共IList Get(表达式条件) { 返回_cach
private readonly Dictionary<Type, List<object>> _cache;
public IList<T> Get<T> (Expression<Func<T, bool>> criteria)
{
return _cache[typeof(T)].Where (criteria);
}
专用只读字典\u缓存;
公共IList Get(表达式条件)
{
返回_cache[typeof(T)]。其中(标准);
}
编译器(正确地)抱怨它不能从object转换为T
我应该如何从那里开始
解决方案
return\u cached[type].AsQueryable().Cast().Where(criteria).ToList()
想法是将列表作为IQueryable,然后我可以使用
private readonly Dictionary<Type, List<object>> _cache;
public IList<T> Get<T> (Expression<Func<T, bool>> criteria)
{
return _cache[typeof(T)].Cast<T>().Where (criteria).ToList();
}
专用只读字典\u缓存;
公共IList Get(表达式条件)
{
返回_cache[typeof(T)].Cast().Where(criteria).ToList();
}
如果不能绝对确定所有元素都是t类型,则可以使用.OfType()
(这将跳过无法强制转换的元素)
编辑当T是valuetype(struct)时,还需要使用.OfType()
编辑由于您的评论提到IQueryable,这可能有助于:
return _cache[typeof(T)].AsQueryable().Cast<T>().Where (criteria).ToList();
return\u缓存[typeof(T)].AsQueryable().Cast().Where(criteria).ToList();
可能正在动态构建表达式,然后进行编译,这是一个更好的选择。以下是一个示例:
Expression<Func<TestClass, bool>> query = LambdaBuilder.BuildQuery(queryItems);
Func<TestClass, bool> compiledExpression = query.Compile();
var results = data.Where(compiledExpression);
expressionquery=lambdbuilder.BuildQuery(queryItems);
Func compiledExpression=query.Compile();
var results=data.Where(compiledExpression);
您可以在上面阅读专用只读词典\u cache;
公共IList Get(表达式条件)
{
返回_cache[typeof(T)].Cast().Where(criteria).ToList();
}
您真的需要参数为表达式>?当然,这不应该起作用:
private readonly Dictionary<Type, List<object>> _cache;
public IList<T> Get<T>(Func<T,bool> criteria)
{
return _cache[typeof(T)].Cast<T>().Where(criteria).ToList();
}
专用只读字典\u缓存;
公共IList获取(函数标准)
{
返回_cache[typeof(T)].Cast().Where(criteria).ToList();
}
好的,修复了它:
return _cached[type].AsQueryable().Cast<T>().Where (criteria).ToList()
return\u cached[type].AsQueryable().Cast().Where(criteria).ToList()
这样做的目的是将列表作为一个IQueryable,然后我可以施展…@Stécy Can you。。。显示带有错误的代码?一定还有别的事on@Stécy:直到在方法声明“System.Collections.Generic.IEnumerable”中添加
where t:class
,它才真正包含“where”的定义和“System.Linq.Queryable.where”(System.Linq.IQueryable,System.Linq.Expressions.Expression)的最佳扩展方法重载“有一些是无效的arguments@Stécy:使用System.Linq
应该有帮助,还是词典中存储了列表以外的内容?(列表不可IQueryable)将确认的解决方案添加到答案中以供将来参考给出以下错误:“System.Collections.Generic.IEnumerable”不包含“Where”的定义和“System.Linq.Queryable.Where”的最佳扩展方法重载(System.Linq.IQueryable,System.Linq.Expressions.Expression)“有一些无效的论点吗?”你的回答引导我走向正确的方向。在强制转换之前,我只添加了AsQueryable(),编译器现在很满意。如果包含System.Linq,错误应该会消失。这是因为在不同的名称空间中有不同的扩展方法,这取决于您是在IQueryable还是IEnumerable上操作。
private readonly Dictionary<Type, List<object>> _cache;
public IList<T> Get<T>(Func<T,bool> criteria)
{
return _cache[typeof(T)].Cast<T>().Where(criteria).ToList();
}
return _cached[type].AsQueryable().Cast<T>().Where (criteria).ToList()