如何动态构建LINQ [英] How do I build up LINQ dynamically

查看:106
本文介绍了如何动态构建LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我有自定义配置的列名称,关联的运算符,如< > =之间等等,然后一个值相关联。



我正在尝试确定是否可以使用动态(字符串)where子句构建LINQ查询?



我注意到Predicate.OR Preditcate.AND的东西,但这不是我在说的。



任何建议?

解决方案

如果您在谈论字符串 其中子句(而不是自己构建表达式) - 然后动态LINQ库(在3.5样本中,IIRC)应该足够了。



请注意,以下示例用于数据库使用;但是您可以通过在内存数据中调用 .AsQueryable()将其与LINQ对象一起使用。



alt text http://www.scottgu.com/blogposts/dynquery/step2.png


I have a scenario where I have custom configured column names, associated operators like < > = between etc. and then a value associated.

I'm trying to determine if it is possible to build up a LINQ query with a dynamic (string) where clause?

I've noticed the Predicate.OR Preditcate.AND stuff, but that is not quite what I'm talking about.

Any suggestions?

解决方案

If you are talking about a string Where clause (rather than building the expression etc yourself) - then the Dynamic LINQ Library (in the 3.5 samples, IIRC) should suffice.

Note that the example below is for database usage; but you can use it with LINQ-to-Objects by calling .AsQueryable() on your in-memory data.

alt text http://www.scottgu.com/blogposts/dynquery/step2.png

这篇关于如何动态构建LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆