过滤掉与条件不同元素 [英] Filter out distinct elements with condition

查看:95
本文介绍了过滤掉与条件不同元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的列表有一个属性有些重复。我想获得的所有非重复的,也是1基于条件的重复的。

I have a list of objects that has some duplicates by a property. I would like to get all non-duplicate and also 1 of the duplicates based on a condition.

有关如:

清单:


  1. 代码:1,等级:10

  2. 代码:1,等级:20

  3. 代码:2,等级:1

预计列表:


  1. 代码:1,等级:20

  2. 代码:2,等级:1

条件将是重复的元素,抢具有最高等级。我怎么会写在lambda或LINQ表达式来做到这一点?

The condition would be that of the duplicate elements, grab the one with the highest Grade. How would I write the lambda or linq expression to do this?

推荐答案

您可以使用 GROUPBY 来做到这一点:

You can use GroupBy to do this:

var results = items.GroupBy(item => item.Code)
                   .Select(g => g.OrderByDescending(i => i.Grade)
                   .First());

这篇关于过滤掉与条件不同元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆