Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取列表中不同对象的计数_C#_Asp.net_List_Class - Fatal编程技术网

C# 如何获取列表中不同对象的计数

C# 如何获取列表中不同对象的计数,c#,asp.net,list,class,C#,Asp.net,List,Class,我有一个C类对象列表,如下所示: { { id=1, date=04-07-2014 } { id=1, date=05-07-2015 } { id=2, date=05-05-2014 } { id=3, date=12-06-2014 } { id=4, date=12-07-2014 } { id=2, date=11-07-2014 } } 如何才能找到列表中属于7月份的每个不同“id”的计数。日期存储为“dd-MM-yyyy”。您需要首先使用日期根据月份对

我有一个C类对象列表,如下所示:

{
  { id=1, date=04-07-2014 }
  { id=1, date=05-07-2015 }
  { id=2, date=05-05-2014 }
  { id=3, date=12-06-2014 }
  { id=4, date=12-07-2014 }
  { id=2, date=11-07-2014 }
} 

如何才能找到列表中属于7月份的每个不同“id”的计数。日期存储为“dd-MM-yyyy”。

您需要首先使用日期根据月份对记录进行分组。对于每个组,您必须按Id分组,然后在字典中选择每个Id及其计数,如:

var query = list.GroupBy(r => r.date.Month)
            .Select(grp => new
                {
                    Month = grp.Key,
                    IdsCount = grp.GroupBy(r => r.id)
                                    .ToDictionary(subGroup => subGroup.Key, 
                                                  subGroup => subGroup.Count()),
                });
如果日期是字符串格式yyyy dd MMTHH:mm:ss,则分组如下:

list.GroupBy(r => DateTime.ParseExact(r.date, "yyyy-dd-MMTHH:mm:ss", CultureInfo.InvariantCulture).Month)
编辑:

要恢复这些值,请执行以下操作:

foreach (var month in query)
{
    Console.WriteLine("For the month of: {0}", CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month.Month));
    foreach (var id in month.IdsCount)
    {
        Console.WriteLine("ID: {0} , Count: {1}", id.Key, id.Value);
    }
}

日期的类型是字符串还是日期时间。你试过什么吗?看看IEnumerableDistinct在哪里被描述谢谢你的回复。但是,这个月没有被确认。“我的日期是这样的格式:2014-12-07T00:00:00 yyyy dd MMThh:mm:ss。@Roshan,你在评论中说它是DateTime类型的对象,为什么要首先将它存储为字符串。但是,您可以将日期字符串解析为DateTime,然后使用Month属性。使用DateTime.ParseExactr.date,yyyy-dd-MMTHH:mm:ss,CultureInfo.InvariantCulture.monthhanks-Habib。很抱歉,事实上,我调用的web api获取列表是存储我使用该格式的日期。但是,如何在var query之外访问字典?@Roshan,类似query.FirstOrDefaultr=>r.Month=7.IdsCount,假设您想访问七月的id,我想获取id和计数。