有关PredicateBuilder多个AND OR的问题
我有一个关于PredicateBuilder的问题,我真的希望你能就如何解决这个问题给我一些建议。我会试着解释一下。
我有一种情况,人们可以根据关键字搜索产品。每个关键字都属于一个关键字组,因此一些实际数据将是:
关键字组/关键字
类型 - 链/
类型 - 手链/
颜色 - 紫色/
颜色 - 绿色
现在我希望得到以下结果:
在每个不同的KeywordGroup之间应该有一个OR。
在KeywordGroup内的每个不同的关键字之间应该有一个AND。
例如,用户想要仅搜索具有Purlple或Green颜色的Bracelets。
这个PredicateBuilder有可能吗?
这是我到目前为止:
================================
/// <summary>
/// Search for products
/// </summary>
/// <param name="itemsPerPage"></param>
/// <returns></returns>
public List<Product> SearchProducts(int from, int max, string sorting, List<Keyword> filter, out int totalitems) {
try {
var predicate = PredicateBuilder.True<Product>();
KeywordGroup previousKeywordGroup = null;
foreach (Keyword k in filter.OrderBy(g=>g.KeywordGroup.SortOrder)) {
if (previousKeywordGroup != k.KeywordGroup) {
previousKeywordGroup = k.KeywordGroup;
predicate = predicate.And(p => p.Keywords.Contains(k));
}
else
predicate = predicate.Or(p => p.Keywords.Contains(k));
}
var products = context.Products.AsExpandable().Where(predicate);
//var products = from p in context.Products
// from k in p.Keywords
// where filter.Contains(k)
// select p;
totalitems = products.Distinct().Count();
if (sorting == "asc")
return products.Where(x => x.Visible == true).Distinct().Skip(from).Take(max).OrderBy(o => o.SellingPrice).ToList();
else
return products.Where(x => x.Visible == true).Distinct().Skip(from).Take(max).OrderByDescending(o => o.SellingPrice).ToList();
}
catch (Exception ex) {
throw ex;
}
}
================================
但它不起作用。
你能帮我吗?
谢谢!
丹尼尔
没有找到相关结果
已邀请:
2 个回复
妒垮
注意在使用谓词
和
的每一行中使用
。
埃庐
和
语句的大清单,你需要将它们组合在一起。 像这样的东西......