2014-03-26 97 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)