[英]How to process multiple where clauses in a LINQ statement?
Thanks to those who answered my last question I got the code below to work which allows the developer to send multiple where clauses to a method which includes each of them in a LINQ statement. 感谢那些回答我上一个问题的人,我得到了下面的代码,它允许开发人员将多个where子句发送到一个方法,该方法在LINQ语句中包含每个子句。 However, how can I get the inclusion of the where clauses to be dynamic?
但是,如何才能将where子句包含在动态中呢? Instead of this:
而不是这个:
return customers
.Where(whereClauses[0])
.Where(whereClauses[1])
.ToList();
something like this (pseudo-code): 像这样的东西(伪代码):
List<Customer> customers = new List<Customer>();
foreach (var whereClause in whereClauses)
{
customers
.Where(whereClause...???)
.ToList();
}
return customers;
Here is the code that works: 这是有效的代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestDynamicLinq2343
{
public class Program
{
static void Main(string[] args)
{
List<Customer> customers = Customer.GetCustomers();
List<Func<Customer, bool>> whereClauses = new List<Func<Customer, bool>>();
whereClauses.Add(c => c.LastName.ToUpper().Contains("A"));
whereClauses.Add(c => c.FirstName.ToUpper().Contains("J"));
foreach (var customer in Customer.GetFilteredCustomers(customers, whereClauses))
{
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<Customer> GetCustomers()
{
List<Customer> customers = new List<Customer>();
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", LastName = "Anderson" });
return customers;
}
public static List<Customer> GetFilteredCustomers(List<Customer> customers, List<Func<Customer, bool>> whereClauses)
{
return customers
.Where(whereClauses[0])
.Where(whereClauses[1])
.ToList();
}
}
}
IEnumerable<Customer> dbCustomers = customers;
foreach (var whereClause in whereClauses)
{
dbCustomers = dbCustomers.Where(whereClause);
}
return dbCustomers.ToList();
maybe interesting extensionMethod: 也许有趣的extensionMethod:
public static class IEnumerableExtension
{
public static void AttachWhereClauses<T>(this IEnumerable<T> source, IEnumerable<Func<T, bool>> whereClauses)
{
foreach (var whereClause in whereClauses)
{
source = source.Where(whereClause);
}
}
}
var listedCustomers = customers.AttachWhereClauses(whereClauses).ToList();
but: not tested - i do not know for sure, if attaching the whereClauses to the same object works! 但是:未经测试 - 我不确定,如果将whereClauses附加到同一个对象上工作!
Something like this: 像这样的东西:
public IEnumerable<Customer> ApplyConditions(IEnumerable<Func<Customer, bool>> conditions)
{
IEnumerable<Customer> customers = ...;
foreach(var condition in conditions)
customers = customers.Where(condition);
return customers;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.