简体   繁体   English

如何检查列表中是否已存在相同的日期?

[英]How can I check if the same Date is already in a List?

I have a List of DateTimes (with Times and Dates) and now need a mothod to get all the unique days out of it into a seperate List 我有一个日期时间列表(有时间和日期),现在需要一个方法来将所有独特的日子带到一个单独的列表中
Example: 例:

[1.1.2015 12:34]    [1.1.2015 12:34]   [1.1.2015 12:34]
[1.2.2015 4:34]     [1.2.2015 2:34]     [1.2.2015 1:34]
[1.3.2015 8:34]     [1.3.2015 1:34]     [1.6.2015 2:34]  

Needs to be Turned into: 需要变成:

[1.1.2015 0:0]    [1.2.2015 0:0]    [1.3.2015 0:0]    [1.6.2015 0:0]

How Do I do this? 我该怎么做呢?

DateTime.Date and Enumrable.Distinct is an option (assuming you don't need to keep order): DateTime.Date和Enumrable.Distinct是一个选项(假设您不需要保持顺序):

var dates = dateAndTimes.Select(d => d.Date).Distinct().ToList();

Distinct may, but does not have to preserve order of items (I believe current implementations will keep order). Distinct可能,但不必保留项目的顺序(我相信当前的实施将保持秩序)。

If you need to formally guarantee original order - either re-sort ( OrderBy ) if list is already sorted, or iterate items in the original list and add to new list once that are not already in the list (similar to what Distinct does internally, but with guaranteed behavior of your code). 如果您需要正式保证原始订单 - 如果列表已经排序,则重新排序( OrderBy ),或者迭代原始列表中的项目并添加到列表中尚未包含的新列表(类似于Distinct内部的操作,但保证您的代码行为)。

Note that as per Tim Schmelter comment for keeping order of items using something like newList.Contains(itemAboutToBeAdded) is very slow (and awkward as you'd need to check if item is in sequence that is currently being constructed), using regular foreach similar to following would math O(n) performance provided by Distinct along with formal order guarantee: 请注意,根据Tim Schmelter对使用类似newList.Contains(itemAboutToBeAdded)类的项目保持顺序的评论非常慢(并且因为您需要检查项目是否按当前正在构建的顺序而变得笨拙),使用类似的常规foreach以下是Distinct提供的数学O(n)表现以及正式的订单保证:

 var dates = dateAndTimes.Select(d => d.Date);
 var alreadyInList = new HashSet<DateTime>();
 var datesInSameOrder = new List<DateTime>();
 foreach(var date in dates)
 {
    if (!alreadyInList.Contains(date))
    {
        alreadyInList.Add(date);
        datesInSameOrder.Add(date);
    }
 }

Shorter version can be written to rely on the fact that HashSet.Add returns false if item is already present: 可以编写较短的版本以依赖HashSet.Add如果item已存在则返回false的事实:

var alreadyInList = new HashSet<DateTime>();
var datesInSameOrder = dates.Where(date => alreadyInList.Add(date)).ToList();

or even if relying on implementation details for order of items in HashSet to be the same as order of Add calls (formally order is not defined, so use at own risk, Distinct have the same behavior - so no benefit): 或者,即使依赖HashSet中项目顺序的实现细节与Add调用的顺序相同(正式顺序未定义,因此使用风险自负, Distinct具有相同的行为 - 所以没有任何好处):

 var datesInSameOrder = new HashSet<DateTime>(dates).ToList();

You can group them on Date property and project all keys: 您可以在Date属性上对它们进行分组并投影所有键:

var dates = dateandTimes.GroupBy(x=> x.Date)
                        .Select(x=>x.Key).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM