Linq2SQL “or/and” operators (ANDed / ORed conditions)

吃可爱长大的小学妹 提交于 2019-11-27 08:16:12

问题


Let's say we need to apply several conditions to select from a table called "Things" (unknown count and nature)

if conditions are known, we can write

db.Things.Where(t=>foo1 && foo2 || foo3);

but if we have to build that Where condition programatically, I can imagine how can we apply ANDed conditions

IQuerable DesiredThings = db.Things.AsQuerable();
foreach (Condition c in AndedConditions)
DesiredThings = DesiredThings.Where(t => GenerateCondition(c,t));

What about ORed conditions ? Note: we don't want to perform union, unique, or any other costly operations, it's desired that a query is generated as if we write it ad-hock

Thanks in advance.


Addition:

PredicateBuilder: Dynamically Composing Expression Predicates


回答1:


You could use the Expression class with static methods to do it run time.

The below code is ment to create a delegate taking one argument called value of type int . It reads from buttom to top so the line in question is:

var method = LambdaExpression.Lambda(orExp, Expression.Parameter(typeof(int), "value"));

the body of the method compares the value of the parameter to a call to method Bar of a newly created object of type foo

var exp2 = Expression.Equal(Expression.Parameter(typeof(int), "value"), Expression.Property(Expression.New(typeof(Foo).GetConstructor(new Type[] { })), "Bar"));

It then creates a similar expression and or's them

        var orExp = Expression.OrElse(exp1, exp2);

final thing is the call to compile. That call generates a delegate that can be used in your where method call.

hope it helps tho Im not 100% sure on the expression to get the value from a parameter

var exp1 = Expression.Equal(Expression.Parameter(typeof(int),"value"), Expression.Property(Expression.New(typeof(Bar).GetConstructor(new Type[] { })), "Foo"));
            var exp2 = Expression.Equal(Expression.Parameter(typeof(int), "value"), Expression.Property(Expression.New(typeof(Foo).GetConstructor(new Type[] { })), "Bar"));
            var orExp = Expression.OrElse(exp1, exp2);
            var method = LambdaExpression.Lambda(orExp, Expression.Parameter(typeof(int), "value"));
            method.Compile();

You might wanna look at invoke for invokation instead of compiling the expression, if you need the LambdaExpression to be translated into something different than binary code (E.g. into an SQL statement)




回答2:


For OR, you have two choices:

  • use Union/Concat
  • write the Expression in code

The second is closer to the .Where(x => {a} || {b}).

If you are using LINQ-to-SQL, you can use Expression.Invoke to combine multiple separate lambda expressions (see this answer) - however, this isn't supported in Entity Framework. In EF, you have to build the entire expression as a single block, using Expression.OrElse; for example here or here.



来源:https://stackoverflow.com/questions/1450983/linq2sql-or-and-operators-anded-ored-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!