2009-11-23 89 views
1

感謝那些回答我的last question的人,我得到了下面的代碼,它允許開發人員將多個where子句發送到包含LINQ語句中的每個子句的方法。 但是,我怎麼能包含where子句是動態的?取而代之的是:如何在LINQ語句中處理多個where子句?

return customers 
     .Where(whereClauses[0]) 
     .Where(whereClauses[1]) 
     .ToList(); 

是這樣的(僞代碼):

List<Customer> customers = new List<Customer>(); 
foreach (var whereClause in whereClauses) 
{ 
    customers 
    .Where(whereClause...???) 
    .ToList(); 
} 
return customers; 

這裏是工作的代碼:

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(); 
     } 
    } 
} 

回答

4
IEnumerable<Customer> dbCustomers = customers; 
foreach (var whereClause in whereClauses) 
{ 
    dbCustomers = dbCustomers.Where(whereClause); 
} 
return dbCustomers.ToList(); 

也許有趣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(); 

但是:沒有測試 - 我不知道肯定,如果將whereClauses附加到相同的對象的作品!

+0

優秀,完美的作品。 – 2009-11-23 10:53:36

+0

:)很高興聽到! – 2009-11-23 11:24:13

0

事情是這樣的:

public IEnumerable<Customer> ApplyConditions(IEnumerable<Func<Customer, bool>> conditions) 
{ 
    IEnumerable<Customer> customers = ...; 
    foreach(var condition in conditions) 
     customers = customers.Where(condition); 
    return customers; 
}