值全部来自下拉列表,可以为空或不为空。 需要编写一个无论是否有任何参数都可以工作的查询 值是否为空。仅当所有参数都满足时,以下才会工作/返回结果 有值并且分别所有 .Where 语句都为真。我在想一些通配符正则表达式来指定允许 null 参数匹配任何内容。因此,参数越多,搜索精度越高,但如果只提供一些或不提供,仍然有效。
public ActionResult GetResults(string age, string ProgType, string Country)
{
var results = _db.Categories
.Where(c => c.CatSchema.TypeName == "Programs")
.Where(c => c.FilterValToCatMaps.Any(fm => fm.FilterValue.Value == age))
.Where(c => c.FilterValToCatMaps.Any(fm => fm.FilterValue.Value == ProgType))
.Where(c => c.RootCat.Name == Country)
.Select(c => c.RootCat);
return View();
}
请您参考如下方法:
您可以有条件地应用“Where”子句 - 例如:
public ActionResult GetResults(string age, string ProgType, string Country)
{
var query = _db.Categories
.Where(c => c.CatSchema.TypeName == "Programs");
if (String.IsNullOrWhiteSpace(age) == false)
{
query = query
.Where(c => c.FilterValToCatMaps.Any(fm => fm.FilterValue.Value == age));
}
if (String.IsNullOrWhiteSpace(ProgType) == false)
{
query = query
.Where(c => c.FilterValToCatMaps.Any(fm => fm.FilterValue.Value == ProgType));
}
if (String.IsNullOrWhiteSpace(Country) == false)
{
query = query
.Where(c => c.RootCat.Name == Country);
}
var result = query
.Select(c => c.RootCat);
return View();
}
