首先你可以简化 if 条件:
public List<PriceRow> GetABSPriceRows(string dekorNr, string bezeichnung, string hersteller)
{
List<PriceRow> lstFoundPriceRows = _lstABSPriceRows; //_lstABSPriceRows is the source list
if (!string.IsNullOrWhitespace(dekorNr))
lstFoundPriceRows = lstFoundPriceRows.FindAll(p => p.Artikelnummer.Contains(dekorNr));
if (!string.IsNullOrWhitespace(bezeichnung))
lstFoundPriceRows = lstFoundPriceRows.FindAll(p => p.Bezeichnung.Contains(bezeichnung));
if (!string.IsNullOrWhitespace(hersteller))
lstFoundPriceRows = lstFoundPriceRows.FindAll(p => p.Lieferant.Contains(hersteller));
return lstFoundPriceRows;
}
其次,您可以使用 where 子句,它实际上不会对列表执行扫描(您现在正在执行 3 次扫描):
public List<PriceRow> GetABSPriceRows(string dekorNr, string bezeichnung, string hersteller)
{
IEnumerable<PriceRow> lstFoundPriceRows = _lstABSPriceRows; //_lstABSPriceRows is the source list
if (!string.IsNullOrWhitespace(dekorNr))
lstFoundPriceRows = lstFoundPriceRows.Where(p => p.Artikelnummer.Contains(dekorNr));
if (!string.IsNullOrWhitespace(bezeichnung))
lstFoundPriceRows = lstFoundPriceRows.Where(p => p.Bezeichnung.Contains(bezeichnung));
if (!string.IsNullOrWhitespace(hersteller))
lstFoundPriceRows = lstFoundPriceRows.Where(p => p.Lieferant.Contains(hersteller));
return lstFoundPriceRows.ToList();
}
既然现在你正在做一次扫描,你可以移动Where谓词中的条件:
public List<PriceRow> GetABSPriceRows(string dekorNr, string bezeichnung, string hersteller)
{
IEnumerable<PriceRow> lstFoundPriceRows = _lstABSPriceRows; //_lstABSPriceRows is the source list
lstFoundPriceRows = lstFoundPriceRows.Where(p => string.IsNullOrWhitespace(dekorNr) || p.Artikelnummer.Contains(dekorNr));
lstFoundPriceRows = lstFoundPriceRows.Where(p => string.IsNullOrWhitespace(bezeichnung) || p.Bezeichnung.Contains(bezeichnung));
lstFoundPriceRows = lstFoundPriceRows.Where(p => string.IsNullOrWhitespace(hersteller) || p.Lieferant.Contains(hersteller));
return lstFoundPriceRows.ToList();
}
由于没有更多的 if,您可以将这些语句合并为一个。
public List<PriceRow> GetABSPriceRows(string dekorNr, string bezeichnung, string hersteller)
{
return _lstABSPriceRows
.Where(p => string.IsNullOrWhitespace(dekorNr) || p.Artikelnummer.Contains(dekorNr))
.Where(p => string.IsNullOrWhitespace(bezeichnung) || p.Bezeichnung.Contains(bezeichnung))
.Where(p => string.IsNullOrWhitespace(hersteller) || p.Lieferant.Contains(hersteller))
.ToList();
}
最后,我们可以将所有Where 谓词归为一个(感谢@Kris):
public List<PriceRow> GetABSPriceRows(string dekorNr, string bezeichnung, string hersteller)
{
return _lstABSPriceRows
.Where(p => (string.IsNullOrWhitespace(dekorNr) || p.Artikelnummer.Contains(dekorNr)) &&
(string.IsNullOrWhitespace(bezeichnung) || p.Bezeichnung.Contains(bezeichnung)) &&
(string.IsNullOrWhitespace(hersteller) || p.Lieferant.Contains(hersteller)))
.ToList();
}
您可以使用如下扩展方法创建一个更简单(并且兼容 3.5)IsNullOrWhitespace:
public static bool IsNullOrWhitespace(this string s)
{
return (s == null || string.IsNullOrEmpty(s.Trim()));
}
有了它,表达式变得更加简单:
public List<PriceRow> GetABSPriceRows(string dekorNr, string bezeichnung, string hersteller)
{
return _lstABSPriceRows
.Where(p => (dekorNr.IsNullOrWhitespace() || p.Artikelnummer.Contains(dekorNr)) &&
(bezeichnung.IsNullOrWhitespace() || p.Bezeichnung.Contains(bezeichnung)) &&
(hersteller.IsNullOrWhitespace() || p.Lieferant.Contains(hersteller)))
.ToList();
}