我正在尝试简化下面的解决方案,其中我正在尝试将 List<DateTime>
转换为 SortedSet<long>
。我想知道这是否可能?
List<DateTime> dateTimes = new List<DateTime>();
dateTimes.Add(...);
// simplify the 5 lines below into 1 line
SortedSet<long> timestamps = new SortedSet<long>();
foreach(DateTime dateTime in dateTimes)
{
timestamps.Add(convertDateTimeToTimestamp(dateTime));
}
我已经成功通过以下方法将 List<float>
转换为 List<double>
:
List<float> average = new List<float>();
average.Add(...);
List<double> newAverage = average.Select(x => (double?)x).ToList();
然而,我找不到.ToSet()
或.ToSortedSet()
方法。
.ToSortedSet()
的原因是什么? - Jon.ToHashSet()
方法,但在 .NET Core 和 .NET Standard 中存在。这似乎是一个新的添加。也许他们将来会添加更多内容。 - ProgrammingLlamapublic static T ToCollection<T,U>(this IEnumerable<U> collection) where T: ICollection<U>, new() {T retval = new T();foreach(U item in collection){retval.Add(item);}return retval;}
。我找不到任何不需要调用者显式指定类型参数的实现方法。 - Brian