C# 我想把它写下来,或者用第一行来解决,但我现在要加上它。选择这个名字也让我感到同样的沮丧是,但这看起来像EF,因此当使用上述查询时,逻辑将实际转换为SQL并在DB.Exception中运行-无法比较“System.String[]”类型的元素。仅支持基元类型
C# 我想把它写下来,或者用第一行来解决,但我现在要加上它。选择这个名字也让我感到同样的沮丧是,但这看起来像EF,因此当使用上述查询时,逻辑将实际转换为SQL并在DB.Exception中运行-无法比较“System.String[]”类型的元素。仅支持基元类型,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我想把它写下来,或者用第一行来解决,但我现在要加上它。选择这个名字也让我感到同样的沮丧是,但这看起来像EF,因此当使用上述查询时,逻辑将实际转换为SQL并在DB.Exception中运行-无法比较“System.String[]”类型的元素。仅支持基元类型、枚举类型和实体类型supported@GuruprasadRao这是在查询之外执行null检查的原因,因为EF不知道如何将其转换为SQL。我可以选择此选项,但我有多个选项要筛选,这种方法效率低下,例如,检查所有过滤器。如果您的if条款很昂贵,
我想把它写下来,或者用第一行来解决,但我现在要加上它。选择这个名字也让我感到同样的沮丧是,但这看起来像EF,因此当使用上述查询时,逻辑将实际转换为SQL并在DB.Exception中运行-无法比较“System.String[]”类型的元素。仅支持基元类型、枚举类型和实体类型supported@GuruprasadRao这是在查询之外执行
null
检查的原因,因为EF不知道如何将其转换为SQL。我可以选择此选项,但我有多个选项要筛选,这种方法效率低下,例如,检查所有过滤器。如果您的if
条款很昂贵,那么这绝对是一个不错的选择。然而,这可能会让你绊倒的一个方法是,如果initialQuery
的类型不相同,以致if
子句中的赋值失败(我认为在这里不会失败,但在一些更复杂的查询中可能会失败)。@GuruprasadRao效率低下,或者只是大量重复的代码?它可以被制作成一个助手方法。@GuruprasadRao:还要注意,您可以链接Where
子句。在迭代最终集合之前,不会对它们进行计算。因此,您可以执行initialQuery=initialQuery.Where(smodel.StatusID…
),然后稍后(在另一个if
子句中)initialQuery=initialQuery.Where(smodel.somethingElse…
让我试试@juharr。。请稍等……)
public class SearchFilters
{
public SearchFilters()
{
MinPrice = "10000";
MaxPrice = "8000000";
}
public IEnumerable<SelectListItem> Categories { get; set; }
public string[] CategoriesId { get; set; }
public IEnumerable<SelectListItem> Locations { get; set; }
public string[] LocationID { get; set; }
public IEnumerable<SelectListItem> Status { get; set; }
public string[] StatusID { get; set; }
public string MinPrice { get; set; }
public string MaxPrice { get; set; }
}
[HttpGet]
public ActionResult Search(SearchFilters smodel)
{
var query=db.tblProperties.Where(p => smodel.StatusID.Contains(p.PropertyLocation)).Select(x=>x).ToList();
//.....
//.....
}
var query=db.tblProperties.Where(p => smodel.StatusID == null || smodel.StatusID.Contains(p.PropertyLocation))
.Select(x=>x).ToList();
.Where(p => smodel.StatusID == null ||
!smodel.StatusID.Any() ||
smodel.StatusID.Contains(p.PropertyLocation))
.Where(p => !smodel.StatusID.Any() ||
smodel.StatusID.Contains(p.PropertyLocation))
var query=db.tblProperties.Where(p => smodel.StatusID == null || smodel.StatusID.Contains(p.PropertyLocation))
.Select(x=>x).ToList();
var query = db.tblProperties
.Where(p => smodel.StatusID == null ||
smodel.StatusID.Contains(p.PropertyLocation))
.ToList();
bool ignore = smodel.StatusID == null || !smodel.StatusID.Any();
var query = db.tblProperties
.Where(p => ignore ||
smodel.StatusID.Contains(p.PropertyLocation))
.ToList();
public static class CollectionExtension
{
public static bool CheckContainsIfHasValue<T>(this IEnumerable<T> source, T value)
{
return source == null || source.Contains(value);
}
}
var query = db.tblProperties
.Where(p => smodel.StatusID.CheckContainsIfHasValue(p.PropertyLocation))
.ToList();
var initialQuery = db.tblProperties;
if(smodel.StatusID != null)
{
initialQuery = initialQuery.Where(p => smodel.StatusID.Contains(p.PropertyLocation));
}
var query = initialQuery.ToList();
public static IEnumerable<T> ConditionalWhere<T>(
this IEnumerable<T> collection,
Func<bool> condition,
Expression<Func<T, bool>> predicate)
{
if(condition())
return collection.Where(predicate);
return collection;
}
var query = db.tblProperties.ConditionalWhere(
() => smodel.StatusID != null,
p => smodel.StatusID.Contains(p.PropertyLocation));
var query = db.tblProperties.ConditionalWhere(
() => smodel.StatusID != null,
p => smodel.StatusID.Contains(p.PropertyLocation))
.ConditionalWhere(
() => someOtherCollection != null,
p => someOtherCollection.Contains(p.PropertyLocation));