gpt4 book ai didi

c# lambda 中的动态 Where 子句

转载 作者:行者123 更新时间:2023-11-30 21:53:12 24 4
gpt4 key购买 nike

我有一个页面允许用户通过多个字段进行搜索。可以一个或全部使用。用户可以将每个字段的运算符设置为等于、包含、开始于等...看起来像这样 enter image description here

我正在使用 EntityFrame Work 并使用像这样的 lamba 检索数据:

listOfPeople = adDB.People.Where(x => x.LastName.StartsWith(lastName) && x.FirstName.StartsWith(firstName)).OrderBy(x => x.LastName)

问题是,如何根据用户提供的数据动态创建 where 子句?

最佳答案

您可以使用类似于 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# lambda 中的动态 Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34097922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com