linq 中的动态表达式
我正在尝试创建一个库,在其中我可以在 xml 文件中指定查询参数和运算符,并且在运行时我可以基于 xml 文件生成 linq 表达式。我让它仅用于构建“where”表达式,以便简单的查询,例如 select * from table where table.id = 1。 这是 xml 文件中的简单查询的示例:
<query name="latest">
<PropertyValue PropertyName="TimeStamp" OperatorName="GreaterThanOrEqual" ParamName="lastUpdated" />
</query>
TimeStamp 属性名称是要使用的 C# 类中的属性。 ParamName 是来自我的 asp.net 应用程序中的 http 请求的 url 参数。 在代码中,我可以由此构建 linq 表达式,并创建以下 where 子句:
(IQueryable<DataObject>)dataObjects.Where(expression);
where 表达式是:
TimeStamp >= "2011-09-21T11:54:24"
但我有一种需要能够处理的新型查询:
select * from theTable t where Id=(select表中的前 1 个 ID 其中 Source=t.Source order by Id desc)
此查询在具有 Id 字段和 Source 字段的表上运行。该查询返回每个源的最新条目。因此,它按源分组并按降序排序,并返回每个源的第一个条目。然后,外部选择返回内部选择中每个结果的所有列。示例:
表:
id source field3 field4
1 Device1 test test
2 Device2 test2 test2
3 Device1 test3 test3
4 Device2 test4 test4
查询结果:
id source field3 field4
3 Device1 test3 test3
4 Device2 test4 test4
所以现在我需要在 where 子句中动态生成嵌套查询。我想我的第一个问题是字符串查询可以转换为 linq 查询吗?然后我需要能够以某种方式动态构建嵌套查询..抱歉,如果它没有多大意义......
I am trying to make a library where I can specify query parameters and operators in an xml file and at runtime i can generate linq expressions based on an xml file. I have it working only on building the "where" expressions so simple queries such as select * from table where table.id = 1. Here is an example of a simple query in the xml file:
<query name="latest">
<PropertyValue PropertyName="TimeStamp" OperatorName="GreaterThanOrEqual" ParamName="lastUpdated" />
</query>
The TimeStamp property name is the name of the Property in the C# class to use. The ParamName is a url parameter coming in from a http request in my asp.net application.
In the code i can build the linq expression out of this and make the following where clause:
(IQueryable<DataObject>)dataObjects.Where(expression);
where expression is:
TimeStamp >= "2011-09-21T11:54:24"
But i have a new type of query that i need to be able to handle:
select * from theTable t where Id=(select top 1 Id from theTable
where Source=t.Source order by Id desc)
This query runs on a table that has an Id field and a Source field. The query returns the newest entry per each Source. So it groups by source and orders it in descending order and returns the first entry for each source. Then the outer select returns all the columns for each of the results in the inner select. Example:
Table:
id source field3 field4
1 Device1 test test
2 Device2 test2 test2
3 Device1 test3 test3
4 Device2 test4 test4
Results of the query:
id source field3 field4
3 Device1 test3 test3
4 Device2 test4 test4
So now i need to dynamically generate a nested query in the where clause. I guess my first question is can that string query be converted to a linq query? Then i need to be able to dynamically build that nested query somehow.. Sorry if it doesn't make a lot of sense...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许 PredicateBuilder 就是您正在寻找的?
Maybe the PredicateBuilder is what you're looking for?