Linq를 사용하여 두 데이터 소스를 동일한 유형으로 변환 한 다음 결합하여 정렬 할 수 있습니다. 여기에 타임 스탬프 필드와 다른 필드가있는 T_Log 데이터베이스의 개체가 있고 다른 소스가 있습니다. 각 문자열에 줄의 시작 부분에 타임 스탬프가 들어있는 가짜 파일의 문자열. 둘 다 사용자 지정 클래스로 변환했습니다.다음이 정렬하는 데 사용됩니다. 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);
감사합니다.지금은 그냥 여기 조금을 선택했고 거기는 :) 작업을 진행하게 내 첫 번째 목록은 목록는 두 번째 목록은 하나 개의 요소 결합 날짜 시간 있는 테이블 클래스이었다 [0] s의 타임 스탬프와 함께했다으로 모두 내가 결국 : var ld = rs.Select (x => 새 KeyValuePair (DateTime.Parse (x [0]), x)) .Concat (ta.Select (y => new KeyValuePair (y.Tidspunkt, y))).) OrderBy (d => d.Key); 그리고 값 유형은 다형성과 같이 (LD에 var에 m) { (m.Value이 Nettbud이다) 경우 ... 다른 ... } 우리가 알려 주셔서 마틴 –
Martin
감사 foreach는 어떻게 지내? 그리고 답을 수락하는 것을 잊지 마십시오. :) –
미안하지만 여기는 처음입니다. 포맷에 대한 지침을 볼 수없고, 600chars를 확장하고 마킹에 응답 할 수 없습니다! Martin – Martin