Using condionals in Linq Programatically
Posted
by Mike B
on Stack Overflow
See other posts from Stack Overflow
or by Mike B
Published on 2010-03-13T17:19:30Z
Indexed on
2010/03/13
17:25 UTC
Read the original article
Hit count: 220
I was just reading a recent question on using conditionals in Linq and it reminded me of an issue I have not been able to resolve. When building Linq to SQL queries programatically how can this be done when the number of conditionals is not known until runtime?
For instance in the code below the first clause creates an IQueryable that, if executed, would select all the tasks (called issues) in the database, the 2nd clause will refine that to just issues assigned to one department if one has been selected in a combobox (Which has it's selected item bound to the departmentToShow property).
How could I do this using the selectedItems collection instead?
IQueryable<Issue> issuesQuery;
// Will select all tasks
issuesQuery = from i in db.Issues
orderby i.IssDueDate, i.IssUrgency
select i;
// Filters out all other Departments if one is selected
if (departmentToShow != "All")
{
issuesQuery = from i in issuesQuery
where i.IssDepartment == departmentToShow
select i;
}
© Stack Overflow or respective owner