3

Basically I wanted to have an if-else statement in my linq to sql statement.

var query = from d in database
            if(x == y) {
                where d.Attr = x
            }
            else {
                 where d.Attr = y
            }
            select d;

Any ideas?

1
  • 2
    I'm guessing that this is just an example, and that you aren't actually using that check, because if you are, it will always resolve the case for the value of 'y'. x=4, y=3 use the else (3) x=3, y=3 use the if (3) Commented May 21, 2010 at 21:11

6 Answers 6

8

Supposing, you meant == and not =:

from d in database
where (x == y && d.Attr == x) ||
      (x != y && d.Attr == y)
select d;
Sign up to request clarification or add additional context in comments.

2 Comments

if x and y are not parameters of database, it would be better to move the if outside the LINQ query
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.
7

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<Foo>()
                                .And( f => f.Attr == y )
                                .And( f => f.Attr == x )
                                .Or( f => f.Attr == z );
var query = db.Foo.Where( predicate );

1 Comment

Hahah well I gave a bad example. Thanks for the answer, this helped a lot.
0

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

Comments

0

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

Comments

0

This is the solution, assuming you meant == and not =:

var query = from d in database
            where (x == 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.

Comments

0

var query = from d in database where (x == y)? d.Attr = x : d.Attr = y select 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.