If i have 3 DateTime lists that come from different sources
List<Datetime> list1 = GetListOfDates();
List<Datetime> list2 = GetAnotherListOfDates();
List<Datetime> list3 = GetYetAnotherListOfDates();
What would be the quickest way to return a list of DateTime that exist in all 3 of the lists. Is there some LINQ statement?
List<DateTime> common = list1.Intersect(list2).Intersect(list3).ToList();
HashSet<DateTime> common = new HashSet<DateTime>( list1 );
common.IntersectWith( list2 );
common.IntersectWith( list3 );
The HashSet
class is more efficient for such tasks than using Enumerable.Intersect
.
Update: make sure all your values are of the same DateTimeKind
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With