I have a list of object called eventQueue
in which i've stored all the event produced by a task executed in background. Now, i need to handle this list extracting specific event from it. I would like to use LINQ mostly for code readability (i wouldn't like to write multiple foreach), my problem is that i don't exactly know how to proceed. Suppose that i have multiple instance of a custom EventArgs
named FirstEventArgs
, and i want to extract a specific one from the list, doing a foreach i would write
foreach(object o in eventQueue)
{
if( o is FirstEventArgs)
{
FirstEventArgs ev = o as FirstEventArgs ;
if( ev.MyProperty == desiredValue)
{
// you got it
}
}
}
currently i was able to write the following in LINQ
FirstEventArgs ev = eventQueue.Where(x => x.GetType() == typeof(FirstEventArgs )).SingleOrDefault() as FirstEventArgs;
my problem is. How do i modify the previous to add the condition ev.MyProperty = desiredValue
in the Where if x is of type object?