I need to do a loop over a bunch of files and spit out log entries through a LINQ query:
foreach (string file in Directory.EnumerateFiles(TextBoxLogDirectory.Text, "*.log"))
{
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using (LogReader reader = new LogReader(stream))
{
var events = (from x in reader.Parse().Where(y => y.IsInRange(range) && (y.EventNo == 1180 || y.EventNo == 1187) && y.GetDefaultMessageField().Contains(":Outbound/"))
group x by x.GetDefaultMessageField() into grouping
select new
{
ID = grouping.Key,
Event1180 = grouping.LastOrDefault(z => z.EventNo == 1180),
Event1187 = grouping.LastOrDefault(z => z.EventNo == 1187)
}).ToList();
}
}
This query must run on a single file at a time, and the above works fine, but I need to keep appending the results of the query to an object outside of the foreach loop. Something like this (although this doesn't work, unfortunately):
dynamic events; // I want to append to this object outside of the loop's scope.
foreach (string file in Directory.EnumerateFiles(TextBoxLogDirectory.Text, "*.log"))
{
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using (LogReader reader = new LogReader(stream))
{
events = (from x in reader.Parse().Where(y => y.IsInRange(range) && (y.EventNo == 1180 || y.EventNo == 1187) && y.GetDefaultMessageField().Contains(":Outbound/"))
group x by x.GetDefaultMessageField() into grouping
select new
{
ID = grouping.Key,
Event1180 = grouping.LastOrDefault(z => z.EventNo == 1180),
Event1187 = grouping.LastOrDefault(z => z.EventNo == 1187)
}).ToList().Concat(events);
}
}
How can I achieve this sort of behavior?
Not sure if it helps, my query above will return objects with (string)ID, (LogEvent)Event1180, and (LogEvent)Event1187.