In the following example, GetFilteredCustomers() works fine so I can send various letters which I want customers to have in their last name.
But how can I build GetFilteredCustomersDynamic() which will enable me to send a full where clause that can be dynamically included in the LINQ statement?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDynamicLinq2343
{
public class Program
{
static void Main(string[] args)
{
List<Customer> 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<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" });
return customers;
}
public static List<Customer> GetFilteredCustomers(List<Customer> customers, string letter)
{
return (from c in customers
where c.LastName.Contains(letter)
select c).ToList();
}
public static List<Customer> GetFilteredCustomersDynamic(List<Customer> customers, Func<..., bool> whereClause)
{
return (from c in customers
where ...whereClause...
select c).ToList();
}
}
}
Answer:
thanks elder_george and arjuns, I got this example to work like this (albeit without the Expression<>
):
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestDynamicLinq2343
{
public class Program
{
static void Main(string[] args)
{
List<Customer> customers = Customer.GetCustomers();
Func<Customer, bool> 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<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-Luc", LastName = "Beaudoir" });
return customers;
}
public static List<Customer> GetFilteredCustomers(List<Customer> customers, Func<Customer, bool> whereClause)
{
return customers
.Where(whereClause).ToList();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…