简体   繁体   English

LINQ to object和通用PredicateBuilder似乎不能很好地配合

[英]LINQ to object and universal PredicateBuilder does not seem to play well together

I have a scenario to use codes below to simplify the explanation here. 我有一种情况,可以在下面使用代码来简化说明。

I have a model class 我有一个模特班

class Model
{
    public string CodeLevel1 { get; set; }
    public string CodeLevel2 { get; set; }
    public bool IsVoluntary { get; set; }
}

It is obvious that I will build a list of objects 很明显,我将建立一个对象列表

        var models = new List<Model>
        {
            new Model()
            {
                CodeLevel1 = "32",
                CodeLevel2 = "A1",
                IsVoluntary = false
            },
            new Model()
            {
                CodeLevel1 = "32",
                CodeLevel2 = "A2",
                IsVoluntary = true
            },
            new Model()
            {
                CodeLevel1 = "33",
                CodeLevel2 = "A3",
                IsVoluntary = true
            },
            new Model()
            {
                CodeLevel1 = "34",
                CodeLevel2 = "A4",
                IsVoluntary = false
            },
            new Model()
            {
                CodeLevel1 = "34",
                CodeLevel2 = "A5",
                IsVoluntary = false
            },
            new Model()
            {
                CodeLevel1 = "34",
                CodeLevel2 = "A6",
                IsVoluntary = true
            },
        };

I want to use PredicateBuilder introduced in A universal PredicateBuilder to build dynamic query. 我想使用通用 PredicateBuilder中引入的PredicateBuilder来构建动态查询。 Following code is just my first step of attempt. 遵循代码只是我尝试的第一步。

var configs = new Dictionary<string, List<string>>()
{
    { "32", new List<string>() { "A1", "A2"} },
    { "33", new List<string>() { "A3" } },
};
var predicate = PredicateBuilder.False<Model>();
var allLevel1CodesInConfig = (from c in configs select c.Key).ToList();
predicate.Or(x => !allLevel1CodesInConfig.Contains(x.CodeLevel1) && x.IsVoluntary == false);
var filteredList = models.AsQueryable().Where(predicate).ToList();

I get nothing in the filteredList , but if I rewrite the last line of code I get what I am expecting. 我在filteredList什么也没得到,但是如果我重写最后一行代码,我将得到期望的结果。

var filteredList = models.AsQueryable().Where(x => !allLevel1CodesInConfig.Contains(x.CodeLevel1) && x.IsVoluntary == false).ToList();

I need some help to understand why the predicate in Where does not work for me? 我需要一些帮助以了解为什么“ Where不适合我”中的谓词的原因?

The problem is in this line: 问题在这一行:

predicate.Or(x => !allLevel1CodesInConfig.Contains(x.CodeLevel1) && x.IsVoluntary == false);

Change it to: 更改为:

predicate = predicate.Or(x => !allLevel1CodesInConfig.Contains(x.CodeLevel1) && x.IsVoluntary == false);

Each PredicateBuilder method creates a new predicate and does not mutate the original one. 每个PredicateBuilder方法都会创建一个新的谓词,并且不会对原始谓词进行变异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM