Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k 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 Answer

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...