如何将where语句发送到在LINQ语句中动态执行的方法?
在下面的示例中, GetFilteredCustomers()工作正常,因此我可以发送我希望客户姓氏的各种字母。
但是我如何构建GetFilteredCustomersDynamic(),这将使我能够发送一个可以动态包含在LINQ语句中的完整where子句?
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestDynamicLinq2343 { public class Program { static void Main(string[] args) { List customers = Customer.GetCustomers(); //semi-dynamic foreach (var customer in Customer.GetFilteredCustomers(customers, "o")) { Console.WriteLine(customer.LastName); } //fully-dyanmic (can send where clauses) foreach (var customer in Customer.GetFilteredCustomersDynamic(customers, c => c.FirstName.Contains("a"))) { Console.WriteLine(customer.LastName); } Console.ReadLine(); } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Street { get; set; } public string Location { get; set; } public string ZipCode { get; set; } public static List GetCustomers() { List customers = new List(); customers.Add(new Customer { FirstName = "Jim", LastName = "Jones" }); customers.Add(new Customer { FirstName = "Joe", LastName = "Adams" }); customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson" }); return customers; } public static List GetFilteredCustomers(List customers, string letter) { return (from c in customers where c.LastName.Contains(letter) select c).ToList(); } public static List GetFilteredCustomersDynamic(List customers, Func whereClause) { return (from c in customers where ...whereClause... select c).ToList(); } } }
回答:
感谢elder_george和arjuns,我让这个例子像这样工作(尽管没有Expression
):
using System; using System.Collections.Generic; using System.Linq; namespace TestDynamicLinq2343 { public class Program { static void Main(string[] args) { List customers = Customer.GetCustomers(); Func whereClause = c => c.LastName.Contains("a") && c.FirstName.Contains("J"); foreach (var customer in Customer.GetFilteredCustomers(customers, whereClause)) { Console.WriteLine(customer.LastName); } Console.ReadLine(); } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Street { get; set; } public string Location { get; set; } public string ZipCode { get; set; } public static List GetCustomers() { List customers = new List(); customers.Add(new Customer { FirstName = "Jim", LastName = "Jones" }); customers.Add(new Customer { FirstName = "Joe", LastName = "Adams" }); customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson" }); customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar" }); customers.Add(new Customer { FirstName = "Jean-Luc", LastName = "Beaudoir" }); return customers; } public static List GetFilteredCustomers(List customers, Func whereClause) { return customers .Where(whereClause).ToList(); } } }
您需要将filter表示为Expression
,而不是Func
。 这样,您可以使用Queryable.Where
方法将此filter添加到LINQ表达式树。
编辑:我错了,因为这段代码使用LINQ到对象,其中委托是适当的过滤条件。 我的错。
例如(更正为使用普通代表):
public static List GetFilteredCustomersDynamic(List customers, Func whereClause) { return customers .Where(whereClause).ToList(); } public static List GetFilteredCustomers(List customers, string letter) { return GetFilteredCustomersDynamic(c => c.LastName.Contains(letter)); }
试试这个代码,
public static List GetFilteredCustomersDynamic(List customers, Expression> whereClause) { return customers.Where(whereClause.Compile()).ToList(); }
@elder_george,有一个拼写错误,表达式应该被编译以获得它的委托,否则这无法编译。
return customers .Where(whereClause).ToList();
应该读作
上述就是C#学习教程:如何将where语句发送到在LINQ语句中动态执行的方法?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
return customers .Where(whereClause.Compile()).ToList();
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1026236.html