这个问题在这里已经有了答案:
如何按对象中的属性排序列表<T> 19答案 class candle { public DateTime date { get; set; } public double open { get; set; } public double high { get; set; } public double low { get; set; } public double close { get; set; } } List<candle> candleList = new List<candle>();假设我已经为candeList添加了许多蜡烛,那么我如何按日期对candleList进行排序呢?
另外,我如何从candleList中删除所有重复的条目?
谢谢
This question already has an answer here:
How to Sort a List<T> by a property in the object 19 answers class candle { public DateTime date { get; set; } public double open { get; set; } public double high { get; set; } public double low { get; set; } public double close { get; set; } } List<candle> candleList = new List<candle>();Assuming I have added many candles to candeList, How can I then sort candleList by date?
Also, how can I remove all duplicate entries from candleList?
Thank you
最满意答案
漂亮的标准,简单的linq。
var candleList = candleList.Distinct().OrderBy(x => x.date).ToList();正如Adam在下面提到的,这只会删除列表中的重复实例,而不是具有所有相同属性值的实例。
您可以实现自己的IEqualityComparer<Candle>作为通过它的选项。 的IEqualityComparer
你可能想看看msdn,并阅读Linq和Enumerable方法: MSDN - 枚举方法
Pretty standard, simple linq.
var candleList = candleList.Distinct().OrderBy(x => x.date).ToList();As Adam mentioned below, this will only remove duplicate instances within the list, not instances which all of the same property values.
You can implement your own IEqualityComparer<Candle> as an option to get passed this. IEqualityComparer
You may want to take a look at msdn, and read up on Linq and Enumerable Methods: MSDN - Enumerable Methods
更多推荐
发布评论