您可以使用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);
非常感謝很多人!現在,我不得不在這裏挑一點,那裏得到它的工作:) 作爲我的第一個名單是名單與S中的時間戳[0] 第二個列表是一個表類的一個元素的日期時間 結合所有我結束了: var ld = rs.Select(x => new KeyValuePair (DateTime.Parse(x [0]),x)) .Concat(ta.Select(y => new KeyValuePair (y.Tidspunkt,y)))。OrderBy(d => d.Key); 而作爲值類型是多態的: 的foreach(在LD VAR米) { 如果(m.Value是Nettbud) ... 其他 ... } 馬丁 –
Martin
2009-12-11 08:33:04
感謝您讓我們知道你怎麼了。並且不要忘記接受答案。 :) – 2009-12-11 08:36:58
抱歉,這是我第一次來這裏。看不到任何格式指南,擴大600chars和標記回答! Martin – Martin 2009-12-11 08:51:46