- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于 PredicateBuilder 的问题,我真的希望你能给我一些关于如何解决这个问题的建议。我将尝试对此进行解释。
我有一个案例,人们可以根据关键字搜索产品。每个关键字都属于一个关键字组,因此一些真实数据将是:
关键字组/关键字
类型 - 链/
类型 - 手链/
颜色 - 紫色/
颜色 - 绿色
现在我想要得到以下结果:
在每个不同的关键字组之间应该有一个 OR。在 KeywordGroup 中的每个不同的关键字之间应该有一个 AND。
例如,用户只想搜索颜色为紫色或绿色的手镯。
这个 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;
}
}
================================
虽然它不起作用。
你能帮帮我吗?
谢谢!丹尼尔
最佳答案
您需要在循环中为每个关键字使用一个临时变量。来自Predicate Builder page :
The temporary variable in the loop is required to avoid the outer variable trap, where the same variable is captured for each iteration of the foreach loop.
试试这个:
foreach (Keyword k in filter.OrderBy(g=>g.KeywordGroup.SortOrder)) {
Keyword temp = k;
if (previousKeywordGroup != k.KeywordGroup) {
previousKeywordGroup = k.KeywordGroup;
predicate = predicate.And(p => p.Keywords.Contains(temp));
}
else
predicate = predicate.Or(p => p.Keywords.Contains(temp));
}
请注意在使用谓词 And
和 Or
的每一行中使用 temp
。
关于c# - PredicateBuilder 多个 AND OR 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5513617/
我正在使用 PredicateBuilder 在我的操作中创建一个搜索/过滤器部分。在这里: [HttpPost] public ActionResult Test(int? cty,
我有代码: var predicate = PredicateBuilder.True(); predicate = predicate.And(x => x.value1 == "1"); p
我有这样一个实体: public class Product() { public string Name { get; set; } } 我想在 Name 属性上执行关键字搜索,以便对它们进
在我见过的谓词生成器的所有示例中,如果您要构建“和”表达式条件,它会显示一个带有 PredicateBuilder.True 的起始表达式,而 PredicateBuilder.False 如果您正在
我想在 Linq 语句中的 Join 中动态生成跨越多个表的谓词。在以下代码片段中,我想使用 PredicateBuilder 或类似的构造来替换以下代码中的“where”语句: 代替: public
List Prs = data.Products .Where(x=> x.ProductColors
我浏览了 PredicateBuilder来源及其实现让我很好奇。让我们看看 Or 方法的实现: public static Expression> Or (this Expression> expr
我正在为我的应用程序创建一个临时报告功能。我将 PredicateBuilder 用于“Where”部分,但现在当我尝试在“Select”部分中使用它时,我无法成功。 代码示例: IQuerya
我想使用“条件”对象动态构建 LinqToSQL 和 EntityFramework 查询子句,并在必要时利用 C# In a Nutshell PredicateBuilder (http://ww
我在使用 PredicateBuilder 将“Or Where”子句动态添加到 LINQ 语句时遇到问题。我将首先解释我要实现的目标。 我有一个倒排索引,用于存储来自一堆链接标题的关键字。我正在使用
我已经下载了 predicate builder并且很难让它与 Entity Framework 一起工作。这是我的代码:v_OrderDetail 是实体 var context = new Ord
我正在使用 PredicateBuilder 创建动态 Where 子句以从 DataTable 查询数据。我有一个字典,其中包含我需要搜索的列名和值。我只是遍历字典,如果键与列名匹配,则将该键和值添
C# in a Nutshell 有一个名为 PredicateBuilder 的免费类,它可以逐个构建 LINQ 谓词 here .这是向谓词添加新表达式的方法的摘录。有人可以解释一下吗? (我已经
我需要了解使用 PredicateBuilder .在几乎每个如何使用它的示例中,它们都显示如下代码: var predicate = PredicateBuilder.True(); if (!st
我有一个包含可变数量字段名称的列表。我想循环遍历此列表并创建一个谓词来过滤在该字段中具有值的所有记录。 foreach (var field in FieldNames) {
我提取了代码库的相关部分来进行说明。我真的被困在一些可能很简单的事情上。 仅供引用,在数据库中,p 和实体1 之间存在一对多关系。 predicate = PredicateBuilder.False
我一直在阅读 Joseph Albahari 关于 C# 4.0 的精彩书籍,我偶然发现了这门课: public static class PredicateBuilder {
我使用 linqToEntities并想添加 OR动态条件。 我知道there is a great library PredicateBuilder by brother Albahari它可以解决
我有一个关于 PredicateBuilder 的问题,我真的希望你能给我一些关于如何解决这个问题的建议。我将尝试对此进行解释。 我有一个案例,人们可以根据关键字搜索产品。每个关键字都属于一个关键字组
鉴于以下示例模型,我需要通过其所有者的属性查询 ITEMS。这是 Entity Framework 集,我想使用 LINQ 来完成。 public class Owner { public
我是一名优秀的程序员,十分优秀!