Как я могу отправить операторы where методу, которые динамически выполняются в операторе LINQ?
В следующем примереGetFilteredCustomers () работает нормально, поэтому я могу отправлять различные письма, которые я хочу, чтобы клиенты имели их фамилию.
Но как я могу построить GetFilteredCustomersDynamic (), который позволит мне отправить полное предложение where, которое может быть динамически включено в оператор LINQ?
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();
}
}
}