斯普利特不同日期的列表 [英] Split a list by distinct date
问题描述
另一个容易的希望。
让我们说我有一个集合是这样的:
列表与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屋!