-1

Possible Duplicate:
Conditional Linq Queries

We're working on a Log Viewer. The use will have the option to filter by user, severity, etc. In the Sql days I'd add to the query string, but I want to do it with Linq. How can I conditionally add where-clauses?

Community
  • 1
  • 1
chetan
  • 51
  • 3
  • 8

1 Answers1

3

Assuming you are working with IEnumerable, like this:

IEnumerable<LogMessage> logs = /* whatever your source is */
if(condition) {
    logs = logs.Where(log => log.Severity == Severity.Error); // or whatever
}

You can also do this multiple times. If your data source is IQueryable use that instead of IEnumerable.

Jon
  • 428,835
  • 81
  • 738
  • 806