LINQ2SQL"或/和"运营商(与运算/或运算条件) [英] Linq2SQL "or/and" operators (ANDed / ORed conditions)

查看:129
本文介绍了LINQ2SQL"或/和"运营商(与运算/或运算条件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我们需要申请几个条件,从一个名为表中选择物联网(未知的数量和性质)

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

但如果我们要建立一个Where条件编程,我可以想像我们如何运用AND的条件

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

在此先感谢。


predicateBuilder:动态撰写防爆pression predicates

PredicateBuilder: Dynamically Composing Expression Predicates

推荐答案

您可以使用静态方法防爆pression类来做到这一点的运行时间。

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

下面code为彪创建一个委托以int类型的一个参数叫做价值 。它读取BUTTOM顶部所以在这行是:

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

该方法的身体参数的值进行比较,以调用Foo类型的新创建的对象

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

然后它会创建一个类似的EX pression和或者是它们

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.

希望它有助于寿林不是100%确定前pression从参数获得的价​​值

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

您可能要编译EX pression的看调用的invokation相反,如果你需要的LambdaEx pression翻译成东西比二进制code(例如到一个SQL语句)不同的

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)

这篇关于LINQ2SQL"或/和"运营商(与运算/或运算条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆