【发布时间】:2017-09-18 15:02:33
【问题描述】:
我有一个数据表,我正在尝试对列实施服务器端过滤。我允许每列查询多个字符串,并试图确定单步执行所有过滤器的最佳方式。下面是一个表格对象的例子,为了简单起见,只有两个参数。
//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,反之亦然。有什么建议?我觉得这是一个明显的答案,我只是忽略了,尽管我搜索的任何其他内容仅适用于单个查询过滤器。提前致谢。
【问题讨论】:
-
我建议你查看
PredicateBuilder类,更多信息在this answer中。
标签: c# linq filter datatable contains