Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

c# - Unable to set paramaters for PredicateBuilder

I thought I was having a completely different issue but in playing around with this I believe I found my true problem. However, I don't know why it's happening.

I'm using PredicateBuilder (LinqKit) to build dynamic queries to pull data based on user input.

Here's where I assign the variables for searching:

            var pb = PredicateBuilder.New<UserData>(true);
            foreach (var item in lstParams)
            {
                string name = "City";// item.spName;
                string val = "Port";// item.spValue;
                pb = pb.And(ud => ud.VarName.Contains(name)).And(ud => ud.VarValue.Contains(val));
            }

The item.spName equals "City" and item.spValue equals "Port". When I run the above code, I do not see "City" and "Port" but instead I see references to the variables:

{ud => (ud.VarName.Contains(value(DAL.DBAccessMethods.DataAccess+<>c__DisplayClass78_0).name) AndAlso ud.VarValue.Contains(value(DAL.DBAccessMethods.DataAccess+<>c__DisplayClass78_0).val))}

In playing around with this, I hardcoded the variables into the PredicateBuilder and the names show up in inspection and the query works.

                pb = pb.And(ud => ud.VarName.StartsWith("City"));
                pb = pb.And(ud => ud.VarValue.StartsWith("bl"));

On inspection I see this:

{ud => (ud.VarName.StartsWith("City") AndAlso ud.VarValue.StartsWith("Port"))}

How do I get variables into the PredicateBuilder object?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The actual query needed to have "AsExpandable()":

                List<UserData> dbRecord = await db.UserData.AsExpandable()
                    .Include(ud => ud.UserKey)
                    .Where(pb).ToListAsync();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...