可以使用的東西沿着Func鍵工廠的線條要做到這一點,因爲在where子句接受一個Func鍵。
實施例:
public class Program
{
public static void Main(string[] args)
{
var people = new[]
{
new Person {FirstName = "Hello", LastName = "World"},
new Person {FirstName = "Foo", LastName = "Bar"},
};
Console.WriteLine(people.Where(FuncFactory.GetFilterFunc<Person>(FilterType.Contains, x => x.FirstName, "ello")).Any());
Console.WriteLine(people.Where(FuncFactory.GetFilterFunc<Person>(FilterType.Equals, x => x.FirstName, "ello")).Any());
Console.WriteLine(people.Where(FuncFactory.GetFilterFunc<Person>(FilterType.Contains, x => x.LastName, "ar")).Any());
Console.WriteLine(people.Where(FuncFactory.GetFilterFunc<Person>(FilterType.Equals, x => x.LastName, "ar")).Any());
Console.ReadKey();
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public enum FilterType
{
Contains,
Equals
}
public static class FuncFactory
{
public static Func<T, bool> GetFilterFunc<T>(FilterType filterType, Func<T, IComparable> propFunc, string filter)
{
switch (filterType)
{
case FilterType.Contains:
return x => (propFunc(x) as string).Contains(filter);
case FilterType.Equals:
return x => (propFunc(x) as string).Equals(filter);
default:
throw new ArgumentException("Invalid FilterType");
}
}
}
的可能的複製[C#LINQ動態選擇與所有類型的數據(http://stackoverflow.com/questions/31789229/c-sharp-linq-dynamic-select-with -all型的數據) – tdbeckett