我们可以从 linq 表达式中删除子句吗?
我想知道是否可以从 linq 表达式/查询运算符添加/删除 where 子句。
例如:-
var qry = from e in emp where(e => e.salary > 5000) select e;
是否可以在稍后阶段删除 where 表达式?
提前致谢 :)
I was wondering whether it's possible to add/remove a where clause from a linq expression/ query operators.
Eg :-
var qry = from e in emp where(e => e.salary > 5000) select e;
Is it possible to remove where expression at a later stage?
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这是可能的,但您需要实现一个
ExpressionVisitor
类来计算复合表达式并根据您的需要更改它。除非您正在做一些相对复杂的事情,否则可能有更好的方法来完成您想要的事情。Yes, it is possible, but you need to implement an
ExpressionVisitor
class to evaluate the composite expression and change it according to your needs. Unless you are doing something relatively complex there is probably a better way to accomplish what you want.据我所知,您只能调整您的过滤,
例如是否尝试根据条件 e.salary > 删除5000,你应该尝试类似的东西
you can only adjust your filtering as far as i know
for example if are trying to delete based on condition e.salary > 5000 , they you should try something like
如果您想以编程方式添加和删除 where 子句,您可以使用查询运算符。
您需要进一步扩展您的问题。
If you want to programmatically add and remove where clauses you can use query operators.
You need to expand on your question a little more.
如果
qry
是IQueryable< /code>
- 例如,LINQ-to-SQL 或 LINQ-to-Entities 查询 - 那么应该可以分析底层表达式树并构建一个不包括
Where< 的新表达式树/代码> 子句。
如果
qry
是普通的IEnumerable;
- 例如,LINQ-to-Objects 查询 - 那么这是无法完成的,因为没有表达式树可供分析。If
qry
is anIQueryable<T>
-- for example, a LINQ-to-SQL or LINQ-to-Entities query -- then it should be possible to analyse the underlying expression tree and build a new one excluding theWhere
clause.If
qry
is a plainIEnumerable<T>
-- for example, a LINQ-to-Objects query -- then this can't be done since there will be no expression tree to analyse.我认为如果有条件查询,则需要
WHERE
子句。否则(如果没有条件)您可以使用:I think
WHERE
clause is required if there is a conditional query. Otherwise (if no condition) you can use: