can someone please help me turn this nested structure into a single LINQ statement?
EventLog[] logs = EventLog.GetEventLogs();
for (int i = 0; i < logs.Length; i++)
{
if (logs[i].LogDisplayName.Equals("AAA"))
{
for (int j = 0; j < logs[i].Entries.Count; j++)
{
if (logs[i].Entries[j].Source.Equals("BBB"))
{
remoteAccessLogs.Add(logs[i].Entries[j]);
}
}
}
}
Nested loops usually end up with multiple “from” clauses (which are converted into calls to
SelectManyby the compiler):(That’s assuming that
remoteAccessLogsis empty before this call, and that you’re happy iterating over it directly – you can callToList()if you want aList<T>.)Here’s the dot notation form:
Or for a list:
Note that I’ve used the overloaded == for string as I find it easier to read than calling the
Equalsmethod. Either will work though.