C# Lambda Statement with Express Test

i am trying to design a LINQ query that filters on a specific location. In my UI dropdown list, that dropdown has the first entry as 'All Sites'. So when the query see's 'All Sites' as the value, it should not perform the filter. This may not make much sense but this is how i used to do it in SQL..

select * from table
where ((location = @loc and @loc is not null) or (@loc is null))

this is how i am trying to do it in LINQ.

var dataSetB = db.Data
.Where(j => loc != 'All Sites' && j.Location1.Description.Equals(loc));

The problem is that when the user selects 'All Sites' - no result are returned when i would be expecting all results not filtered by location.

Does anyone know how to accomplish this in one LINQ statement?

example dropdown..

<All Sites>
<New York>


Your dropdown wouldn't normally have just text, it will normally be bound to text/value pairs. So your text entry that reads New South Wales would have a corresponding value of NSW. The entry that reads All Sites would have a value of null. So you could change your query to:

public object GetMyStuff(string loc)
    var dataSetB = db.Data
                     .Where(j => loc == null || j.Location1.Description == loc);

Logical shortcutting applies in the expression, so the j.Location1.Description will only be compared if the passed loc value is not null.

Using neutral location values is important - you don't really want to be comparing the literal text All Sites for a bunch of reasons.

Not entirely sure this will work:

var dataSetB = db.Data
  .Where(j => loc != 'All Sites' ? j.Location1.Description.Equals(loc) : true);

Need Your Help

AngularJS - Remove table row after object destroyed

jquery angularjs

I have an AngularJS app that returns a list of json objects and I iterate through those objects and place them into a table.

Objective-C and Windows

objective-c cocoa ide

I am currently in a class that is developing applications in Objective-C and Cocoa. I was wondering if there was any way to build and compile Objective-C applications on Windows Vista. During class...