c#在lambda中的动态Where子句 [英] c# Dynamic Where clause in a lambda

查看:251
本文介绍了c#在lambda中的动态Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我正在使用EntityFrame工作,并使用这样的lamba检索数据:

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

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

解决方案

您可以使用Func工厂的一些内容来执行此操作,因为where子句在Func中。



示例: p>

  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;组; }
public string LastName {get;组; }
}

public enum FilterType
{
包含
等于
}

public static class FuncFactory
{
public static Func< T,bool> GetFilterFunc< T(FilterType filterType,Func< T,IComparable> propFunc,string filter)
{
switch(filterType)
{
case FilterType.Contains:
返回x => (propFunc(x)作为字符串).Contains(filter);
case FilterType.Equals:
return x => (propFunc(x)作为字符串).Equals(filter);
default:
throw new ArgumentException(Invalid FilterType);
}
}
}


I have a page that allow the user to search by multiple fields. One can be used or all. The user can set the operator per field to Equals, Contains, Starts With, etc... Looks like this

I am using EntityFrame Work and retrieving the data using a lamba like this one:

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

The question, How do I dynamically create the where clause depending on the data provided by the user?

解决方案

You can use something along the lines of a Func factory to do this since the where clause takes in a Func.

Example:

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子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆