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;
}
9
  • 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? Commented Aug 28, 2012 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). Commented Aug 28, 2012 at 20:28
  • @TimSchmelter check out the first "var result = (from EventLogEntry elog in el.Entries where ......) Commented Aug 28, 2012 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"). Commented Aug 28, 2012 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? Commented Aug 28, 2012 at 20:34

3 Answers 3

1

.Where() takes an expression:

if (!String.IsNullOrEmpty(sc.Message))
    query = query.Where(elog => elog.Message.Contains(sc.Message));
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting the error: Cannot convert type IEnumerable<EventLogEntry> to IOrderedEnumerable<EventLogEntry>. An explicit conversion exists, are you missing a cast? Let me try some more tweaking....
Do your ordering after you build up your where conditions
Hmmm, changing it back to this answer got rid of my exceptions....thanks jrummell
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.

Comments

0

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

where (elog.InstanceId == 4107 || elog.InstanceId == 4108)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.