久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 條件)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 自拍偷拍视频网 | 欧美三级视频在线观看 | 久久er99热精品一区二区 | 欧州一区二区 | 精品综合久久久 | 一区二区三区欧美 | 欧美精品一区三区 | 国产精品中文字幕在线播放 | 久久综合成人精品亚洲另类欧美 | 精品一级 | 国产成人福利视频在线观看 | 天天弄天天操 | 一级黄色毛片免费 | 操久久久 | 亚洲夜射 | 四虎影音| 亚洲国产精品一区二区久久 | 一级片子 | 欧美激情一区 | 亚洲一本 | 久精品久久 | 欧美高清一区 | 色婷婷影院 | 亚洲精品日韩一区二区电影 | 美女视频.| 91免费视频 | 污视频在线免费观看 | 一区二区视频在线 | www性色 | 国产精品美女久久久久aⅴ国产馆 | 一区二区三区在线免费观看 | 欧美综合一区 | 日韩成年人视频在线 | 日韩精品一区二区三区中文在线 | 久久国产精品久久久久久 | 日本精品一区二区 | 日本在线网站 | 视频一区二区国产 | 精品一区电影 | 亚洲免费观看 | 国产精品一区二区三区免费观看 |