我如何通过每个对象的成员对对象列表进行排序?(How can I sort a list of objects by a member of each object? [duplicate])

编程入门 行业动态 更新时间:2024-10-27 16:29:10
我如何通过每个对象的成员对对象列表进行排序?(How can I sort a list of objects by a member of each object? [duplicate])

这个问题在这里已经有了答案:

如何按对象中的属性排序列表<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

更多推荐

本文发布于:2023-07-06 07:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   成员   列表   sort   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!