Linq to sort lists of various kinds

I am trying to make a log viewer that displays events from different sources, but ordered by timestamp. I have logging, I can use C # Linq for this, but how?

example: I have one list of events read from files in a strig list sorted by date date.

Another source of events is database inserts, for which I use Linq to retrieve the same time segment as the first list. The database also has a time stamp.

I want the list to show all the events listed, how they happen in real time. that is, inserting dabase can lead to an error registered in the disk file after a second.

I suppose I'm looking for a way to join and sort them in lists that share only one common timestamp field, with the result that I can use foreach even if each item can be of a different type.

Any ideas?

Martin

+3
source share
3 answers

Linq , . T_Log, Timestamp , - , . CommonLog, . CommonLog , , , .

, KeyValuePair<DateTime, object>.

:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{    
    // Fake database class.
    class T_Log
    {
        public DateTime Timestamp { get; set; }
        public string Info { get; set; }
        public int Priority { get; set; }
    }

    static void Main(string[] args)
    {
        // Create some events in the fake database.
        List<T_Log> dbLogs = new List<T_Log> {
            new T_Log { Timestamp = new DateTime(2009, 2, 5), Info = "db: foo", Priority = 1 },
            new T_Log { Timestamp = new DateTime(2009, 2, 9), Info = "db: bar", Priority = 2 }
        };

        // Create some lines in a fake file.
        List<string> fileLogs = new List<string> {
            "2009-02-06: File foo",
            "2009-02-10: File bar"
        };


        var logFromDb =
            dbLogs.Select(x => new CommonLog(
                          x.Timestamp,
                          string.Format("{1} [Priority={2}]",
                                        x.Timestamp,
                                        x.Info,
                                        x.Priority),
                          x));

        var logFromFile =
            fileLogs.Select(x => new CommonLog(
                            DateTime.Parse(x.Substring(0, x.IndexOf(':'))),
                            x.Substring(x.IndexOf(':') + 2),
                            x
                ));

        var combinedLog = logFromDb.Concat(logFromFile).OrderBy(x => x.Timestamp);
        foreach (var logEntry in combinedLog)
            Console.WriteLine("{0}: {1}", logEntry.Timestamp, logEntry.Log);
    }
}

// This class is used to store logs from any source.
class CommonLog
{
    public CommonLog(DateTime timestamp,
                     string log,
                     object original)
    {
        this.Timestamp = timestamp;
        this.Log = log;
        this.Original = original;
    }

    public DateTime Timestamp { get; private set; }
    public string Log { get; private set; }
    public object Original { get; private set; }
}

:

05-02-2009 00:00:00: db: foo [Priority=0]
06-02-2009 00:00:00: file: baz
09-02-2009 00:00:00: db: bar [Priority=0]
10-02-2009 00:00:00: file: quux

: , - . :

var ld = rs.Select(x => new KeyValuePair<DateTime, object>(DateTime.Parse(x[0]), x))
           .Concat(ta.Select(y => new KeyValuePair<DateTime, object>(y.Tidspunkt, y)))
           .OrderBy(d => d.Key); 
+1

, , .

, db .

    List<Db> db_inserts = new List<Db>();
    // populate list of db events here

    List<Fi> files = new List<Fi>();
    // populate list of file events here

    List<object> all = new List<object>();
    all.AddRange(db_inserts.Cast<object>());
    all.AddRange(files.Cast<object>());

    // sort the list by time
    all.Sort(delegate(object a, object b)
    {
        DateTime aTime = DateTime.MinValue;
        DateTime bTime = DateTime.MinValue;

        if (a is Db)
            aTime = ((Db)a).time;
        else if (a is Fi)
            aTime = ((Fi)a).time;

        if (b is Db)
            bTime = ((Db)b).time;
        else if (b is Fi)
            bTime = ((Fi)b).time;

        return aTime.CompareTo(bTime);
    });

EDIT: (, LogItem , @Mark Byers:

    List<LogItem> all = new List<LogItem>();
    all.AddRange(db_inserts.Select(x => new LogItem { time = x.time, msg = x.name, source=x}));
    all.AddRange(files.Select(x => new LogItem{time = x.time, msg = x.name, source = x}));

    var query = all.OrderBy(x => x.time);
+1

Combine log sources, then sort the results. Assuming each log source has a meaning IEnumerable<LogEntry>:

var logSources = new []
{
    GetFileLogs(),
    GetDbLogs()
    // whatever other sources you need...
};

var entries = Enumerable.Empty<LogEntry>();
foreach (var source in logSources)
{
    entries = entries.Concat(source);
}

entries = entries.OrderBy(e => e.Timestamp);
+1
source

Source: https://habr.com/ru/post/1725351/


All Articles