tags:

views:

204

answers:

1

I have a linq statement that I want to add an additional where clause to if a drop down index is not 0.

people.Where(n.surname == "surname" || n.forename == "forename" && (dropdown.SelectedIndex > 0) ? n.id = dropdown.SelectedValue : n.id  > 0).Select(n => n);

I am not even sure if what I am trying is possible??

I would like to do this rather than having to write two different statements.

Any ideas?

Thanks

+7  A: 

Fortunately, this is easy because queries compose:

var query = people.Where(n.surname == "surname" || n.forename == "forename");
if (dropdown.SelectedIndex > 0)
{
    query = query.Where(n => n.id.ToString() == dropdown.SelectedValue);
}
Jon Skeet
Looks like id is an integer, but the SelectedValue property is a string. One or the other will need to be converted.
tvanfosson
getting an error "cannot be inferred from usage error in my the if statement query = query??
Rigobert Song
I needed paramater in the wherequery = query.Where(n => n.id == dropdown.SelectedValue);Thanks guys
Rigobert Song
Don't you love this feature? It's like dynamic SQL but with less evil :)
borisCallens
it is SWEET!!!!!!!
Rigobert Song
(Fixed both errors in the answer. Love to know what the downvote is for.)
Jon Skeet