斯普利特不同日期的列表 [英] Split a list by distinct date

查看:102
本文介绍了斯普利特不同日期的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个容易的希望。



让我们说我有一个集合是这样的:

 列表与LT; DateTime的> allDates; 



我想将其转换成

 列表<名单,LT; DateTime的>> dividedDates; 



其中'dividedDates每个列表包含属于一个独立的allDates所有日期一年。



有一点LINQ挂羊头卖狗肉,我的疲惫的心灵不能挑出现在呢?



解决方案



接受的答案是正确的。



谢谢,我不ŧ想我是知道GROUPBY的进位的,我试图用.GroupBy()之类的方法,而不是像语法的SQL。并感谢确认了ToList()修正案,并把它包含在接受的答案: - )


解决方案

 变种q =从日期allDates 
组由日期到date.Year datesByYear
选择datesByYear.ToList();
q.ToList(); //返回列表<名单,LT; DateTime的>>


Another easy one hopefully.

Let's say I have a collection like this:

List<DateTime> allDates;

I want to turn that into

List<List<DateTime>> dividedDates;

where each List in 'dividedDates' contains all of the dates in 'allDates' that belong to a distinct year.

Is there a bit of LINQ trickery that my tired mind can't pick out right now?

Solution

The Accepted Answer is correct.

Thanks, I don't think I was aware of the 'into' bit of GroupBy and I was trying to use the .GroupBy() sort of methods rather than the SQL like syntax. And thanks for confirming the ToList() amendment and including it in the Accepted Answer :-)

解决方案

var q  = from date in allDates 
         group date by date.Year into datesByYear
         select datesByYear.ToList();
q.ToList(); //returns List<List<DateTime>>

这篇关于斯普利特不同日期的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆