Linq2SQL“或/和"运算符(ANDed/ORed 条件)

Linq2SQL quot;or/andquot; operators (ANDed / ORed conditions)(Linq2SQL“或/和运算符(ANDed/ORed 条件))
本文介绍了Linq2SQL“或/和"运算符(ANDed/ORed 条件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设我们需要应用几个条件来从名为事物"(未知数量和性质)的表中进行选择

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

ORed 条件怎么样?注意:我们不想执行联合、唯一或任何其他昂贵的操作,希望生成查询就像我们临时编写的一样

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: 动态组合表达式谓词

推荐答案

您可以使用带有静态方法的 Expression 类来实现它的运行时.

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

以下代码用于创建一个委托,该委托采用一个名为 int 类型的值的参数.它从底部到顶部读取,因此有问题的行是:

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 类型对象的方法 Bar 的调用进行比较

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

最后一件事是调用编译.该调用会生成一个委托,可在您的 where 方法调用中使用.

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

希望它有助于我不是 100% 确定表达式从参数中获取值

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

如果您需要将 LambdaExpression 转换为不同于二进制代码的内容(例如转换为 SQL 语句),您可能想查看 invoke for invokation 而不是编译表达式

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“或/和"运算符(ANDed/ORed 条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)