LINQ conditional statement where?

I have a linq statement that I want to add an extra where clause if the 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'm not even sure if what I'm trying to do is possible?

I would like to do this, and not write two different statements.

Any ideas?

thanks

+3
source share
1 answer

Fortunately, this is easy because the queries add up:

var query = people.Where(n.surname == "surname" || n.forename == "forename");
if (dropdown.SelectedIndex > 0)
{
    query = query.Where(n => n.id.ToString() == dropdown.SelectedValue);
}
+11
source

Source: https://habr.com/ru/post/1709676/


All Articles