簡體   English   中英

查看幾個DateTime數組中是否存在任何匹配的最簡單方法是什么?

[英]What is the easiest way to seeing if there are any matches across a few DateTime arrays?

如果我有3個來自不同來源的DateTime列表

 List<Datetime> list1 = GetListOfDates();
 List<Datetime> list2 = GetAnotherListOfDates();
 List<Datetime> list3 = GetYetAnotherListOfDates();

返回所有3個列表中存在的DateTime列表的最快方法是什么。 有一些LINQ聲明嗎?

List<DateTime> common = list1.Intersect(list2).Intersect(list3).ToList();
HashSet<DateTime> common = new HashSet<DateTime>( list1 );
common.IntersectWith( list2 );
common.IntersectWith( list3 );

與使用Enumerable.Intersect相比, HashSet類對於此類任務更有效。

更新:確保所有值都是相同的DateTimeKind

var resultSet = list1.Intersect<DateTime>(list2).Intersect<DateTime>(list3);

您可以與列表相交:

var resultSet = list1.Intersect<DateTime>(list2);
var finalResults = resultSet.Intersect<DateTime>(list3);

foreach (var result in finalResults) {
   Console.WriteLine(result.ToString());
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM