C# - 如何在对象列表上执行多个包含过滤器

时间:2017-04-21 14:43:28

标签: c# linq filter datatable contains

我有一个数据表,我正在尝试为列实现服务器端过滤。我允许多列字符串每列查询,并试图确定跨步所有过滤器的最佳方法。下面是表对象的示例,为简单起见,只有2个参数。

//table models from db
public class Parts
{
    public string PartName { get; set; }
    public virtual Series Series { get; set; }
}

public class Series
{
    public string SeriesName { get; set; }
}

//This is what is passed in from the datatable filter query
public class PartsTable
{
    public string PartName { get; set; }

    public string SeriesName { get; set; }
}


public IEnumerable<Parts> PartsTableSearch(PartsTable table)
{
    //Queries come in as comma separated string
    var partNameList = table.PartName?.Split(',');
    var seriesNameList = table.SeriesName?.Split(',');

    //Gets and generates the list of Parts
    var fullList = GetParts();

    if (partNameList != null && partNameList.Length > 0)
    {        
        foreach (var partName in partNameList)
        {
            fullList = fullList.Where(p => p.PartName.ToLower().Contains(name.ToLower()));
        }
    }
    if (seriesNameList != null && seriesNameList.Length > 0)
    {
        foreach (var seriesName in seriesNameList)
        {
            fullList = fullList.Where(p => p.Series.SeriesName.ToLower().Contains(seriesName.ToLower()));
        }
    }

    return fullList;
}

这对我想要的东西不起作用,因为对于每个参数(即PartName),我需要能够返回所有具有PartName的对象,该PartName包含partNameList中的搜索字符串,然后从该结果中过滤进一步在包含seriesNameList中的搜索字符串的SeriesNames上,然后返回结果集。但是,partNameList的查询可能是空白的,只能搜索seriesName,反之亦然。有什么建议?我觉得这是一个明显的答案,我只是忽略了,虽然我搜索过的任何其他东西只适用于单个查询过滤器。提前谢谢。

4 个答案:

答案 0 :(得分:5)

这可以通过两种方式使用单个.Where来完成:

使用.Contain() 将返回完全匹配:

public IEnumerable<Parts> PartsTableSearch(PartsTable table)
{
    //Queries come in as comma separated string
    var partNameList = table.PartName?.Split(',');
    var seriesNameList = table.SeriesName?.Split(',');

    //Gets and generates the list of Parts
    var fullList = GetParts()
        .Where(p => partNameList.Contains(p.PartName.ToLower())
            || seriesNameList.Contains(p.Series.SeriesName.ToLower()))
        .ToList();

    return fullList;
}

使用.Any() 将返回部分匹配:

public IEnumerable<Parts> PartsTableSearch(PartsTable table)
{
    //Queries come in as comma separated string
    var partNameList = table.PartName?.Split(',');
    var seriesNameList = table.SeriesName?.Split(',');

    //Gets and generates the list of Parts
    var fullList = GetParts()
        .Where(p => partNameList.Any(n => n.Contains(p.PartName.ToLower())))
            || seriesNameList.Any(n => n.Contains(p.Series.SeriesName.ToLower())))
        .ToList();

    return fullList;
}

答案 1 :(得分:0)

为LinqToSql做了这个。我被告知它不适用于Entity Framework。它应该与内存中的集合一起使用。

    public static Expression<Func<T, bool>> OrTheseFiltersTogether<T>(
        this IEnumerable<Expression<Func<T, bool>>> filters)
    {
        Expression<Func<T, bool>> firstFilter = filters.FirstOrDefault();
        if (firstFilter == null)
        {
            Expression<Func<T, bool>> alwaysTrue = x => true;
            return alwaysTrue;
        }

        var body = firstFilter.Body;
        var param = firstFilter.Parameters.ToArray();
        foreach (var nextFilter in filters.Skip(1))
        {
            var nextBody = Expression.Invoke(nextFilter, param);
            body = Expression.OrElse(body, nextBody);
        }
        Expression<Func<T, bool>> result = Expression.Lambda<Func<T, bool>>(body, param);
        return result;
    }

这样称呼:

public IEnumerable<Parts> PartsTableSearch(PartsTable table)
{
    var fullList = GetParts();
    var filters = new List<Expression<Func<Part, bool>>>();

    if (table.PartName != null)
    {
        var partNameList = table.PartName.ToLower().Split(',');
        foreach (var partName in partNameList)
        {
            filters.Add(p => p.PartName.ToLower().Contains(partName));
        }
    }
    if (table.SeriesName != null)
    {
        var seriesNameList = table.SeriesName.ToLower().Split(',');
        foreach (var seriesName in seriesNameList)
        {
            filters.Add(p => p.Series.SeriesName.ToLower().Contains(seriesName));
        }
    }

    Expression<Func<Part, bool>> filter = filters.OrTheseFiltersTogether();

    return fullList.Where(filter);
}

答案 2 :(得分:0)

对于您从不同查询获得的每个结果集使用Yield return。如果它为空则无关紧要,所有结果都将在IEnumerable<Parts>中收集。

public IEnumerable<Parts> PartsTableSearch(PartsTable table)
{
    //Queries come in as comma separated string
    var partNameList = table.PartName?.Split(',');
    var seriesNameList = table.SeriesName?.Split(',');

    //Gets and generates the list of Parts
    var fullList = GetParts();

    if (partNameList != null && partNameList.Length > 0)
    {        
        foreach (var partName in partNameList)
        {
            yield return fullList.Where(p => p.PartName.ToLower().Contains(name.ToLower()));
        }
    }
    if (seriesNameList != null && seriesNameList.Length > 0)
    {
        foreach (var seriesName in seriesNameList)
        {
            yield return fullList.Where(p => p.Series.SeriesName.ToLower().Contains(seriesName.ToLower()));
        }
    }

  yield return null;
}

答案 3 :(得分:0)

这就是我最终让它发挥作用的方式。

var fullList = GetParts();        
if (partNameList != null && partNameList.Length > 0)
        {
            var tempList = new List<Parts>();
            foreach (var partName in partNameList)
            {
                tempList.AddRange(fullList.Where(p => p.PartName.ToLower().Contains(partName.ToLower().Trim())));
            }
            fullList = tempList;
        }
        if (seriesNameList != null && seriesNameList.Length > 0)
        {
            var tempList = new List<Parts>();
            foreach (var seriesName in seriesNameList)
            {
                tempList.AddRange(fullList.Where(p => p.Series.SeriesName.ToLower().Contains(seriesName.ToLower().Trim())).ToList());
            }
            fullList = tempList;
        }
return fullList.ToList();