I've found examples of how to "dynamically" add more where clauses to a linq query, but I'm not sure how to make it work with my specific query.
Here's one example, and here's another. Now here's a look at the query that I have:
var result = (from EventLogEntry elog in el.Entries
where (elog.InstanceId == 4107)
orderby elog.TimeGenerated descending
select elog).ToList();
var query = from EventLogEntry elog in el.Entries
orderby elog.TimeGenerated descending
select elog;
The first example shows the hard-coded InstanceID == 4107
, but I want to be able to add in more where clauses. All of the examples I've seen say to do:
query = query.Where(el.Entries => el.Entries.Message.Contains("error"));
or something like that. But my query is setting defining "the source" as an EventLogEntry object, so when I try and extend the where clause, it doesn't recognize the fields that I want to do my where on....
Any help with this?
This is what I tried to add, and intellisense doesn't recognize any of it:
if (!String.IsNullOrEmpty(sc.Message))
query = query.Where(elog.Message.Contains(sc.Message));
Updated (this is the working version):
var query = from EventLogEntry elog in el.Entries select elog;
try
{
if (!String.IsNullOrEmpty(sc.Message))
query = query.Where(elog => elog.Message.Contains(sc.Message));
query = query.OrderBy(elog => elog.TimeGenerated);
var result = query.ToList();
}
catch
{
throw;
}