[英]What is the easiest way to compare DateTime in linq with sql server ce?
[英]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.