简体   繁体   English

如何通过比较列表中的实例将列表中的列表分为几部分

[英]how to divide list in parts from a list by comparison on the instance inside the list

suppose my list<struct> 假设我的list<struct>

in list some struct have the date 31 january 在列表中,某些结构的日期为1月31日

some have date 5 march 有些约会3月5日

some have 12 august then 有些则是8月12日

i want to make a list of all same date struct 我想列出所有相同日期的结构

how i can do this in c# 我如何在C#中做到这一点

Assume your struct looks like this: 假设您的结构看起来像这样:

struct YourStruct 
{
    DateTime DateProperty { get; set; }
}

Then you can use GroupBy to get the dates: 然后,您可以使用GroupBy获取日期:

List<YourStruct> list = ....;
var dates = list.GroupBy(s => s.DateProperty.Date);

Group by groups on unique values, so you need to group on the Date property of the DateTime instance. 按唯一值按组分组,因此您需要对DateTime实例的Date属性进行分组。 The code above will return an IEnumerable<IGrouping<DateTime>> , where the key of each group will be the corresponding date. 上面的代码将返回IEnumerable<IGrouping<DateTime>> ,其中每个组的键将是相应的日期。

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

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