Linq , . T_Log, Timestamp , - , . CommonLog, . CommonLog , , , .
, KeyValuePair<DateTime, object>.
:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
class T_Log
{
public DateTime Timestamp { get; set; }
public string Info { get; set; }
public int Priority { get; set; }
}
static void Main(string[] args)
{
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 }
};
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);
}
}
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);