If-else equivalent via linq-to-sql query in where clause c?

Isn't that just the same as var query = from d in database where d. Attr == y select d Your situation (or a similar, more realistic one) can be handled with a simple conditional in the where clause. For more complex queries you can use the extension methods to build up a query implementing conditionals pretty easily or use a PredicateBuilder to create arbitrarily complex conditions var query = db.

Table; if (x == y) { query = query. Where( d. Attr == z ); } else { query = query.

Where( d. Attr == t ); } var predicate = PredicateBuilder.True() . And( f => f.

Attr == y ) . And( f => f. Attr == x ) .

Or( f => f. Attr == z ); var query = db.Foo. Where( predicate ).

Isn't that just the same as var query = from d in database where d. Attr == y select d; Your situation (or a similar, more realistic one) can be handled with a simple conditional in the where clause. For more complex queries you can use the extension methods to build up a query implementing conditionals pretty easily or use a PredicateBuilder to create arbitrarily complex conditions.

Var query = db. Table; if (x == y) { query = query. Where( d.

Attr == z ); } else { query = query. Where( d. Attr == t ); } var predicate = PredicateBuilder.True() .

And( f => f. Attr == y ) . And( f => f.

Attr == x ) . Or( f => f. Attr == z ); var query = db.Foo.

Where( predicate ).

Hahah well I gave a bad example. Thanks for the answer, this helped a lot. – Darcy May 25 '10 at 18:27.

Supposing, you meant == and not =: from d in database where (x == y && d. Attr == x) || (x! = y && d.

Attr == y) select d.

If x and y are not parameters of database, it would be better to move the if outside the LINQ query – BlueRaja - Danny Pflughoeft May 21 '10 at 22:03 @BlueRaja define "better" – Frank Krueger May 21 '10 at 23:51 Since x==y will have the same truth value for all elements of database, there is no reason to translate both d. Attr == x and d. Attr == y to SQL.

– BlueRaja - Danny Pflughoeft May 22 '10 at 4:20.

This has been answered already stackoverflow.com/questions/11194/condit....

So vote to close, don't waste an answer slot. – Frank Krueger May 21 '10 at 21:09 Not enough reputation – gcores May 21 '10 at 21:12.

EDIT: I think I may have misunderstood what you wanted to do. I'm not sure if the? (ternary) operator will work in Linq to SQL, but try this: from d in database select (x == y)?

X : y.

Wouldnt if(x == y) { where d. Attr = x } be the same as if(x == y) { where d. Attr = y } if x==y?

So couldnt you just use where d. Attr = y.

D. Attr == x : d. Attr == y) select d; However, this is logically equal to the following: var query = from d in database where d.

Attr == y select d; Because, if x == y is true, then d. Attr == x and d. Attr == y will be equal.

Var query = db. Query = query. Query = query.

Var predicate = PredicateBuilder. And( f => f. And( f => f.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions