[英]Implementing conditional 'if' statements within a LINQ queries 'where' statement
我試圖找出一種在我的數據模型中查詢對象的方法,並且只包括那些非空的參數。 如下所示:
public List<Widget> GetWidgets(string cond1, string cond2, string cond3)
{
MyDataContext db = new MyDataContext();
List<Widget> widgets = (from w in db.Widgets
where
... if cond1 != null w.condition1 == cond1 ...
... if cond2 != null w.condition2 == cond2 ...
... if cond3 != null w.condition3 == cond3 ...
select w).ToList();
return widgets;
}
由於小部件表可能變得非常大,我想避免這樣做:
public List<Widget> GetWidgets(string cond1, string cond2, string cond3)
{
MyDataContext db = new MyDataContext();
List<Widget> widgets = db.Widgets.ToList();
if(cond1 != null)
widgets = widgets.Where(w => w.condition1 == cond1).ToList();
if(cond2 != null)
widgets = widgets.Where(w => w.condition2 == cond2).ToList();
if(cond3 != null)
widgets = widgets.Where(w => w.condition3 == cond3).ToList();
return widgets;
}
我看了幾個例子,但沒有看到任何與我需要做的事情相符的事情。
您要避免的是實際執行查詢,直到您准備好:
public List<Widget> GetWidgets(string cond1, string cond2, string cond3)
{
MyDataContext db = new MyDataContext();
var widgets = db.Widgets;
if(cond1 != null)
widgets = widgets.Where(w => w.condition1 == cond1);
if(cond2 != null)
widgets = widgets.Where(w => w.condition2 == cond2);
if(cond3 != null)
widgets = widgets.Where(w => w.condition3 == cond3);
return widgets.ToList();
}
請注意如何刪除ToList
調用。 在您開始迭代之前,不會執行查詢。 調用ToList
將強制執行此操作,以便可以將結果放入List<>
並返回。 我甚至建議將方法的返回值更改為IEnumerable<Widget>
並最后跳過ToList
調用:
public IEnumerable<Widget> GetWidgets(string cond1, string cond2, string cond3)
{
MyDataContext db = new MyDataContext();
var widgets = db.Widgets;
if(cond1 != null)
widgets = widgets.Where(w => w.condition1 == cond1);
// [...]
return widgets;
}
這樣調用代碼就可以決定何時執行查詢(甚至可以在執行此操作之前添加更多條件)。
使用“或門”:在每個小部件條件測試前加上“||” 並檢查我們是否正在使用該條件。 如果我們不是,那么“或”的后半部分不會被評估。 這就是為什么它是一個門 - 如果第一部分評估為真,我們就不會再進一步了。
如果我正在寫它,我會像下面這樣做。 我使用var syntatic sugar來保存LINQ查詢並將ToList()移動到最后。
public List<Widget> GetWidgets(string cond1, string cond2, string cond3)
{
MyDataContext db = new MyDataContext();
var widgets = from w in db.Widgets
where (cond1 == null || w.condition1 == cond1)
&& (cond2 == null || w.condition2 == cond2)
&& (cond3 == null || w.condition3 == cond3)
select w;
return widgets.ToList();
}
編輯:語法
這樣的事怎么樣?
IEnumerable<Widget> condQuery = (from w in db.Widgets);
if(cond1 != null ) condQuery = condQuery.Where(w=> w.condition1 == cond1);
if(cond2 != null ) condQuery = condQuery.Where(w=> w.condition2 == cond2);
等等...?
您實際上是在 linq查詢中要求調度程序。 Where
方法采用謂詞,因此您可以在創建查詢之前構建謂詞。
- 編輯 - 起初,我認為這更容易,寫了一些甚至沒有編譯的偽代碼。 現在,無論如何,我想我明白了。 這段代碼可行; 它將where子句與應用它分開。
static Predicate<Widget> combine(
Predicate<Widget> existing,
Predicate<Widget> condition )
{
var newpred = new Predicate<Widget>( w=> existing(w) && condition(w) );
return newpred;
}
並使用這樣的“建築”功能:
static void Main(string[] args)
{
string cond1 = "hi";
string cond2 = "lo";
string cond3 = null;
var pr = new Predicate<Widget>( (Widget w ) => true );
if (cond1 != null) pr = combine( pr, w => w.condition1 == cond1);
if (cond2 != null) pr = combine( pr, w => w.condition2 == cond2);
if (cond3 != null) pr = combine( pr, w => w.condition3 == cond3);
我用一個小輔助數組測試它:
var widgets = new Widget[]{
new Widget (){ condition1 = "" },
new Widget (){ condition1 = "hi", condition2 = "lo" }
};
var selected = widgets.Where( (w) => pr(w));
foreach (var w in selected) {
Console.WriteLine(w);
}
我們可以使用下面非常簡單的方法。
(from e in employee
join d in departments on e.departmentId equals d.departmentId
Select new {
e.name,
d.name,
getEmployeeContacts(e)
}
//return active contact if not return first . This is same like if else along with null check
private contact getEmployeeContacts(Employee e )
{
return e.Contacts.FirstOrDefault(x => x.Active == 1) ?? e.Contacts.FirstOrDefault();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.