1

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;
}
Community
  • 1
  • 1
ganders
  • 7,285
  • 17
  • 66
  • 114
  • `so when I try and extend the where clause, it doesn't recognize the fields that I want to do my where on` 1) Can you show the exact code you tried? 2) Can you show the exact error you got? – Mark Byers Aug 28 '12 at 20:27
  • Can you elaborate this more: _"But my query is setting defining "the source" as an EventLogEntry object"_ Show what you mean(pseudo code can clarify your requirement). – Tim Schmelter Aug 28 '12 at 20:28
  • @TimSchmelter check out the first "var result = (from EventLogEntry elog in el.Entries where ......) – ganders Aug 28 '12 at 20:30
  • @ganders: First, it should be `el => el.Entries.Message.Contains("error")`. You have `el.Entries` on left of `=>`. Second, if you want the message, do `elog => elog.Message.Contains("error")`. – Jeff Yates Aug 28 '12 at 20:34
  • @MarkByers I added what I tried to do, it tells me "elog" does not exist in the current context. Which is true because it's only defined in the original declaration of the query variable. How do I "make" it recognize it? – ganders Aug 28 '12 at 20:34
  • It's worth mentioning that you'll want to call `ToList` at the end, after you've finished generating the query. You're just doing pointless work by eagerly evaluating it repeatedly. – Servy Aug 28 '12 at 20:49
  • @Servy yep, I've got the last line doing this: var result = query.ToList(); – ganders Aug 28 '12 at 21:00
  • @ganders The first line of your code doesn't really do anything. You could just write `var query = el.Entries.AsEnumerable()` or `IEnumerable query = el.Entries`. – Servy Aug 28 '12 at 21:20
  • Wow, looking back at my own question a year later, I really didn't know what I was doing with linq back then...haha – ganders Feb 21 '14 at 13:20

3 Answers3

1

Where just takes a boolean result expression. You can use ands, ors, comparators, even mathematical equations. Simply use logical operators to extend the expression within the parenthesis.

1

.Where() takes an expression:

if (!String.IsNullOrEmpty(sc.Message))
    query = query.Where(elog => elog.Message.Contains(sc.Message));
jrummell
  • 42,637
  • 17
  • 112
  • 171
  • I'm getting the error: Cannot convert type IEnumerable to IOrderedEnumerable. An explicit conversion exists, are you missing a cast? Let me try some more tweaking.... – ganders Aug 28 '12 at 20:35
  • 2
    Do your ordering after you build up your where conditions – Mark Peters Aug 28 '12 at 20:43
  • Hmmm, changing it back to this answer got rid of my exceptions....thanks jrummell – ganders Aug 28 '12 at 21:14
0

You can add more expressions just like when you are using an if:

where (elog.InstanceId == 4107 || elog.InstanceId == 4108)
Erwin
  • 4,757
  • 3
  • 31
  • 41