Is there a better, more elegant and concise way to get the intersection of two lists in C #?
In C #, a method for calculating the intersection of a list of dates:
public List<DateTime> dates_common(Timeserie ts1, Timeserie ts2) { var dt1 = new HashSet<DateTime>(ts1.dates); var dt2 = new HashSet<DateTime>(ts2.dates); dt1.IntersectWith(dt2); var dt = new DateTime[dt1.Count]; dt1.CopyTo(dt); return new List<DateTime>(dt); }
In Ruby, this can be done like this:
def dates_common(ts1, ts2) dt1 = ts1.dates.to_set dt2 = ts2.dates.to_set return dt1.intersection(dt2).to_a end
The main reason for this awkwardness is the asymmetry between IEnumerable and specific containers and arrays.
I am constantly amazed at how poorly designed the standard C # libraries are, because such problems occur all the time.
Is there any better that means a more elegant and concise way to do this?
source share