C#动态Linq变量Where子句 [英] C# Dynamic Linq Variable Where clause

查看:358
本文介绍了C#动态Linq变量Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Scott Gu的文章,以创建动态LINQ http: //weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

I am following Scott Gu's article to create a dynamic LINQ http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

他举了一个例子:

Expression<Func<Customer, bool>> e1 = 
    DynamicExpression.ParseLambda<Customer, bool>("City = \"London\"");  
Expression<Func<Customer, bool>> e2 =
    DynamicExpression.ParseLambda<Customer, bool>("Orders.Count >= 10");  
IQueryable<Customer> query =
    db.Customers.Where("@0(it) and @1(it)", e1, e2);  

在我看来,这很好.但是我不知道数目的where子句,这是在运行时决定的.

This works fine in my case. However I have unknown number of where clauses, which is decided at runtime.

任何人都可以告诉我如何创建通用的Where子句,例如

Can anyone please tell me how to create a generic Where clause, such as

Where("@0(it) and @1(it) and... @n(it)", e1, e2, ... en);

谢谢

推荐答案

您可以在query对象上附加其他运算符:

You can attach additional operators on the query object:

 query = db.Customers.Where( ... );
 query = query.Where( ... );
 query = query.Where( ... );

这样,您可以动态附加子句,并且独立于子句的数量.

This way you can attach clauses dynamically and you are independent on their count.

这篇关于C#动态Linq变量Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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