I am trying to make a log viewer displaying events from different sources but ordered by a time stamp. I have a felling I can use C# Linq for this but how ?
example: I have one list of events read from files into a strig list sorted by a datetime stamp.
Another source of events are database inserts for which I use Linq to extract the same time segment as the first list. The database also have a time stamp.
What I want is to have list showing all events listed as they happen in real time. i.e a dabase insert may lead to an exception logged in a disk file a second later.
I guess I am looking for a way to join and sort these to lists sharing only one common field the timestamp, ending up with a collection I can use foreach over even though each element could be of different kind.
Any Ideas ?
Martin
You can use Linq to transform both data sources to the same type, and then combine them and sort them. Here I have some objects from a pretend database table T_Log which has a Timestamp field and some other fields, and another source is some strings from a fake file where each string contains a timestamp at the start of the line. I transformed them both to a custom class
CommonLogand then used this to sort. The CommonLog contains a reference to the original objects so if I need more detailed information I can cast and get that information.A more lightweight implementation could convert to a class that already exists, such as
KeyValuePair<DateTime, object>.Here’s the code:
Output:
Update: Martin replied the following in a comment to this post, but it was hard to read due to lack of formatting in comments. Here it is with formatting: