0

I am trying to convert a complex sql statement into a linq lambda expression.

Here is an example of my sql - statement:

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 

at least I am freaking out about the brackets in the combined or statement. Need some help to convert this statement into linq expression. I cant fire the native sql on the server, because we have a complex linq expression (about 3000 lines of code :-X) we can´t convert it to sql.

In conclusion: I NEED THE LINQ EXPRESSION.

2 Answers 2

3

You can easily do it by using Contains in Linq, as the example below shows:

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

I'm not quite sure about the placement of brackets because I'm not at a development machine, but using Contains is probably the best way to do it

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it´s working fine! could be easier? :-D Thanks a lot mate!
1
where ... new[] {2, 3, 4, 5}.Contains(d)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.