C# remove entries from list based on a distinct field

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers Make propertyBs a HashSet<string> instead of a List<string> , and use the return value of Add to make a decision: var propertyBs = new HashSet<string>(); var res = models.Where(m => propertyBs.Add(m.PropertyB)).ToList(); When you call Add on hash set, only the addition of

C#根据不同的字段从列表中删除条目

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 使propertyBs成为HashSet<string>而不是List<string> ,并使用Add的返回值做出决定: var propertyBs = new HashSet<string>(); var res = models.Where(m => propertyBs.Add(m.PropertyB)).ToList(); 当您调用Add on hash set时,只有添加初始项返回true ; 所有重复的添加都会返回false ,所以它们的相应模型会被滤除。 我可

List with distinct values inside only

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers 如果你想通过Id区分元素: foreach (var x in mydata) { var model = new MyViewModel() { Id = x.Days.Id, Name = x.Days.Name }; if(!models.Contains(x=>x.Id==model.Id) model

仅在内部列出不同的值

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 如果你想通过Id区分元素: foreach (var x in mydata) { var model = new MyViewModel() { Id = x.Days.Id, Name = x.Days.Name }; if(!models.Contains(x=>x.Id==model.Id) models.Add(model); } 你可以使用HashSet

How can I use Linq to get distinct results filtered by property

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers List<Tool> distinctTool = tools .GroupBy(p => p.Number) .Select(g => g.OrderByDescending(q => q.Source == "M1").First()) .ToList(); 您需要按照编号对Tools进行分组,并获取其中的First() List<Tool> distinctTool = tools .GroupBy(p => p.Number) .Select(g => g.Order

我如何使用Linq获取按属性过滤的独特结果

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 List<Tool> distinctTool = tools .GroupBy(p => p.Number) .Select(g => g.OrderByDescending(q => q.Source == "M1").First()) .ToList(); 您需要按照编号对Tools进行分组,并获取其中的First() List<Tool> distinctTool = tools .GroupBy(p => p.Number) .Select(g => g.OrderByDescending(q => q.Source == "

Remove Duplicate Titles from list in MVC

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers First note it is likely that you have duplicates in your database, so maybe you should simply delete them there in the first place. If you are sure that you want to remove duplicates from the result of your query, then depending on how you MediaRating class looks like, you can use Linq's

从MVC列表中删除重复标题

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 首先请注意,您的数据库中可能存在重复项,所以也许您应该首先将其删除。 如果你确信你想从您的查询结果删除重复值,然后根据你MediaRating类的样子,你可以使用LINQ的Distinct方法或DistictBy从MoreLinq(或从约翰飞碟双向的帖子)。 请注意, Distinct方法可能会重新排序元素(这取决于Linq提供程序的实现),因此应该在排序之前调用它。 另一方

Linq Distinct() is not working

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers 使用GroupBy而不是Distinct

Linq Distinct()不起作用

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 使用GroupBy而不是Distinct

How to get distinct value from array of object in C#?

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers storingAll.GroupBy(x=>x.skuID).Select(group=>group.First()); Note that this solution uses no external libraries. Simple group by the first occurrence of skuID. Reference: How to get a distinct list from a List of objects?

如何从C#中的对象数组中获取不同的值?

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 storingAll.GroupBy(x=>x.skuID).Select(group=>group.First()); 请注意,此解决方案不使用外部库。 由第一次出现skuID的简单组。 参考:如何从对象列表中获取不同的列表?

Use linq distinct function?

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers You could use GroupBy on an anonymous type with both properties to make them distinct: var coursesList = from c in not_subsribe group c by new { Id = c.Cours.ID, Name = c.Cours.Name } into g order by g.Key.Name select new SelectListItem { Text = g.Key.Name,

使用linq独特的功能?

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 您可以在具有两种属性的匿名类型上使用GroupBy ,以使它们不同: var coursesList = from c in not_subsribe group c by new { Id = c.Cours.ID, Name = c.Cours.Name } into g order by g.Key.Name select new SelectListItem { Text = g.Key.Name, Value = g.Key.Id }; 这是有效的,因为匿名类型

C# Distinct List from a List

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers You can either use GroupBy with an anonymous type as a key: occupancyResultList = occupancyResultList .GroupBy(x => new { x.on_date, x.type_code, x.available }) .Select(g => g.First()) .ToList(); Or DistinctBy method: occupancyResultList = occupancyResultList

来自列表的C#不同列表

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 您可以使用GroupBy作为关键字的匿名类型: occupancyResultList = occupancyResultList .GroupBy(x => new { x.on_date, x.type_code, x.available }) .Select(g => g.First()) .ToList(); 或DistinctBy方法: occupancyResultList = occupancyResultList .DistinctBy(x => new { x.on_date, x.type_code, x

Linq to get distinct property

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers Rewrite your GetHashCode() function: public int GetHashCode(MY_DATA_TABLE obj) { return obj.CODE.GetHashCode(); } Rule is, that both Equals and GetHashCode() should check the same properties, and you checking just Code in Equals() and whiole object in GetHashCode() 另一种选择是如果Versio

Linq获得独特的财产

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 重写你的GetHashCode()函数: public int GetHashCode(MY_DATA_TABLE obj) { return obj.CODE.GetHashCode(); } 规则是,Equals和GetHashCode()都应该检查相同的属性,并且只需检查GetHashCode()中的Equals()和whiole对象中的代码, 另一种选择是如果Version不重要,则在每个组中进行一个组并取其中的第一个值。 var mapCodes = (from mtc in GetA

Lambda Distinct Select

This question already has an answer here: LINQ's Distinct() on a particular property 17 answers If you don't override Equals and GetHashCode in your class or provide a custom equality comparer Distinct method uses default equality comparer for the type.And it compares reference types by references. Not by the property values. If you don't want this behaviour either override the

Lambda独特选择

这个问题在这里已经有了答案: LINQ对特定属性的Distinct()17的答案 如果您不在类中重写Equals和GetHashCode ,或者提供自定义的相等比较器,则Distinct方法使用默认的相等比较器作为类型。并且它通过引用来比较引用类型。 不是通过属性值。 如果你不希望这种行为覆盖你的类中的相关方法,或者如果你不能改变类实现一个类型的IEqualityComparer并传递给Distinct : var ResourceTypeNameList = Resources .Select(r =