List<History> data = new List<History>(){
new History() {Symbol="a", Close = 1.0m, Date = new DateTime(2016, 2, 1) },
new History() {Symbol="a", Close = 1.2m, Date = new DateTime(2016, 2, 2) },
new History() {Symbol="a", Close = 1.3m, Date = new DateTime(2016, 2, 3) },
new History() {Symbol="b", Close = 1.2m, Date = new DateTime(2016, 2, 1) },
new History() {Symbol="b", Close = 1.2m, Date = new DateTime(2016, 2, 2) },
new History() {Symbol="b", Close = 1.3m, Date = new DateTime(2016, 2, 3) },
};
var StockGroupList = data
.GroupBy(o => o.Symbol)
.OrderBy(o => o.Key)
.ToList();
在“ 2016,2,2”处调用“ a”的“ Close”值的最简单方法是什么,是否可以使用类似StockGroupList [0] [1] .close的方法?
此外,如何订购第二维?例如,对于每个“符号”,按日期顺序排序?
最佳答案
让我们向后编写代码。
为了使StockGroupList [0] [1]工作,StockGroupList的类型应为List<List<History>>
。
var StockGroupList = data
... // Should be type IEnumerable<List<History>>
.ToList();
=>
var StockGroupList = data
.Select( o => o.ToList()) // Should be ordered by Date
.ToList();
=>
var StockGroupList = data
... // Should be IEnumerable<IEnumerable<History>>
.Select( o => o.Orderby(c => c.Date).ToList())
.ToList();
因为
IGrouping<out TKey, out TElement> : IEnumerable<TElement>
,最终代码是var StockGroupList = data
.GroupBy(o => o.Symbol)
.OrderBy(o => o.Key)
.Select(o => o.OrderBy(x => x.Date).ToList())
.ToList();
您可以将
ToList
替换为ToArray
以获得相同的效果。