问题:
I am playing with LINQ to learn about it, but I can't figure out how to use Distinct when I do not have a simple list (a simple list of integers is pretty easy to do, this is not the question). 我正在使用LINQ进行学习,但是当我没有简单的列表时(我很容易做到简单的整数列表,这不是问题),我无法弄清楚如何使用Distinct。 What I if want to use Distinct on a list of an Object on one or more properties of the object? 如果要在对象的一个或多个属性的对象列表上使用Distinct ,该怎么办?
Example: If an object is Person , with Property Id . 示例:如果一个对象是Person ,具有Property Id 。 How can I get all Person and use Distinct on them with the property Id of the object? 如何获取所有Person并在其上使用Distinct和对象的属性Id ?
Person1: Id=1, Name="Test1"
Person2: Id=1, Name="Test1"
Person3: Id=2, Name="Test2"
How can I get just Person1 and Person3? 我怎样才能得到Person1和Person3? Is that possible? 那可能吗?
If it's not possible with LINQ, what would be the best way to have a list of Person depending on some of its properties in .NET 3.5? 如果LINQ无法实现,那么根据.NET 3.5中Person某些属性来获取Person列表的最佳方法是什么?
解决方案:
参考一: https://stackoom.com/question/23HG/LINQ在特定属性上的Distinct参考二: https://oldbug.net/q/23HG/LINQ-s-Distinct-on-a-particular-property
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4315199