2014-03-26 35 views
0

我想將一個複雜的sql語句轉換爲一個linq lambda表達式。將sql語句組合到linq lambda表達式中

這裏是我的SQL的一個例子 - 聲明:

select * from Kunden 
where a=1 
and b=1 
and c=1 
and ( 
( 
(d=1 or d in (2, 3, 4, 5)) <--- 
and e in (1, 2, 3) 
) 
or 
( 
(f=1 or f in (2, 3, 4, 5)) <--- 
and g in (1, 2, 3) 
) 
) 
) 
and h=1 
and i=1 

至少我嚇壞了有關合並或聲明的括號。 需要一些幫助將此語句轉換爲linq表達式。因爲我們有一個複雜的linq表達式(大約3000行代碼:-X),所以我們不能將它轉換爲sql。

結論:我需要LINQ表達式。

回答

3

您可以輕鬆地通過使用Linq中包含做到這一點,如下例所示:

List<int> valuesOne = new List<int> { 2,3,4,5 }; 
List<int> ValuesTwo = new List<int> { 1,2,3 }; 

var myLinq = (from k in Kunden 
      where k.a == 1 && k.b == 1 && k.c == 1 && 
      ((k.d == 1 || ValuesOne.Contains (k.d)) && 
         ValuesTwo.Contains (k.e))) || 
      // now do the same for f 

我不太肯定支架的位置,因爲我不是在發展機器,但使用Contains可能是最好的辦法

+0

謝謝你的工作正常!可能會更容易? :-D多謝隊友! – Ipad

1
where ... new[] {2, 3, 4, 5}.Contains(d)