if-else equivalent via linq-to-sql query in where clause c#


if-else equivalent via linq-to-sql query in where clause c#



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?


how to select a radio button by default - asp.net mvc strongly typed html helpers

1:

Should We Instantiate Data Context & Repository In Controller?
Isn't this just the same as. How can I pass parameters to an Action using Html.Action() in ASP.NET MVC?
var query = from d in database             where d.Attr == y             select d; 
Your position (or a similar, more realistic one) must be handled with a simple conditional in the where clause. The model item passed into the dictionary is of type { ASP.NET MVC } For more complex queries you must use the extension methods to build up a query implementing conditionals pretty easily or use a PredicateBuilder to create arbitrarily complex conditions.. Problem with ASP.Net MVC SelectLIst and List<SelectListItems>
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 ); 
MVC area and virtual foldersMaintain Constant Title Across Website

2:

Which is better for showing lots of small values in ASP.Net MVC - HTML helper or partial view
Supposing, you meant == and not =:.
from d in database where (x == y && d.Attr == x) ||       (x != y && d.Attr == y) select d; 

3:

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

4:

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 

5:

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..

6:

var query = from d in database where (x == y)? d.Attr = x : d.Attr = y select d;.


79 out of 100 based on 54 user ratings 949 reviews