[英]Select Distinct rows based a particular column value in list
I have the following list Lsit<Car> lstcarIP
it has the following data 我有以下列表
Lsit<Car> lstcarIP
它具有以下数据
ID | Name | Year
0 - Zen - 1990
1 - Alto - 2003
3 - Zen - 2004
4 - Santro - 2000
5 - Alto - 2003
Irrespective of the ID and year the output list<Car> lstFinal
should have 不论ID和年份如何,输出
list<Car> lstFinal
应该具有
ID | Name | Year
0 - Zen - 1990
1 - Alto - 2003
4 - Santro - 2000
or 要么
ID | Name | Year
3 - Zen - 2004
4 - Santro - 2000
5 - Alto - 2003
ie If the Name occurs again then only one entry should be added to the list<Car> lstFinal
. 即,如果再次出现“名称”,则应仅将一个条目添加到
list<Car> lstFinal
。 I tried using LastorDefault
or GroupBy
我尝试使用
LastorDefault
或GroupBy
lstFinal= lstcarIP.GroupBy(s => s.Name)
.Where(g => g.Count() > 1)
.SelectMany(g => g)
.ToList<Car>();
But couldnt get proper result. 但是无法获得适当的结果。 Could you correct me and point my mistake.
你能纠正我并指出我的错误吗? Thanks for the help!
谢谢您的帮助! :)
:)
You just GroupBy
, and then get First
您只需
GroupBy
,然后获得First
lstFinal = lstcarIP.GroupBy(s => s.Name)
.Select(g => g.First())
.ToList<Car>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.