LINQ To SQL 语句的动态程度如何?
我需要在运行时根据用户的输入构建 LINQ To SQL 语句,但我似乎不知道如何动态构建 WHERE 子句。
我对以下内容没有问题:
string Filters = "<value>FOO</value>";
Where("FormattedMessage.Contains(@0)",Filters)
但我真正需要的是使整个 WHERE 子句动态化。这样我可以在运行时添加多个条件,如下所示(粗略的想法):
foreach (Filter filter in filterlist)
{
whereclause = whereclause + "&& formattedmessage.contains(filter)";
}
I have the need to construct a LINQ To SQL statement at runtime based on input from a user and I can't seem to figure out how to dynamically build the WHERE clause.
I have no problem with the following:
string Filters = "<value>FOO</value>";
Where("FormattedMessage.Contains(@0)",Filters)
But what I really need is to make the entire WHERE clause dynamic. This way I can add multiple conditions at runtime like this (rough idea):
foreach (Filter filter in filterlist)
{
whereclause = whereclause + "&& formattedmessage.contains(filter)";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以考虑使用 PredicateBuilder 类。使用它可以让您动态地将 AND/OR 条件添加到树中。
请参阅http://www.albahari.com/nutshell/predicatebuilder.aspx
You may also consider using the PredicateBuilder class. Using that will allow you to dynamically add AND/OR conditions to your tree.
Refer to http://www.albahari.com/nutshell/predicatebuilder.aspx
我不知道这里使用了什么数据类型,但是为什么不尝试使用通用查询呢?
这将使用 AND 连接所有条件(如您的问题中所示)。
I don't know what data types are being used here, but why don't you try to use general query?
this will concatenate all the conditions using AND (as is in your question).