[英]How to pass an Expression into a LINQ query?
I can pass an Expression into the LINQ Select() method: 我可以将Expression传递给LINQ Select()方法:
public IQueryable<T> GetInfo(long limit, Expression<Func<MyType, T>> selector)
{
return DbSet.Where(t => t.id < limit).Select(selector)
}
How can I do the same using LINQ-style query? 如何使用LINQ样式的查询来做同样的事情?
public IQueryable<T> GetInfo(long limit, Expression<Func<MyType, T>> selector)
{
var result = from t in DbSet
where t.id < limit
select selector(t) // doesn't work
return result
}
Well you can use: 那么你可以使用:
var result = (from t in DbSet
where t.id < limit
select t).Select(selector);
... but you can't just use the selector inside the query expression, as that's implicitly wrapping an extra layer that you don't want. ...但你不能只使用查询表达式中的选择,做为所隐含包装,你不想额外的层。
It's not clear why you want to use query expressions here anyway, mind you - I would strongly recommend that you become comfortable with both styles of LINQ and use whatever's more appropriate at the time - which in this case is the style you have in your original code. 目前尚不清楚为什么你想在这里使用查询表达式,请注意 - 我强烈建议你对这两种LINQ样式感到满意并使用当时更合适的东西 - 在这种情况下你是原始样式码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.