简体   繁体   中英

How to select grouped greater values from list with LINQ

I have object list (name is list) with eg next values:

[0] {Id='04239',Color='03',MatPre='145698',Stz=210,Spp=1}
[1] {Id='04239',Color='03',MatPre='145698',Stz=210,Spp=3}
[2] {Id='04239',Color='KB',MatPre='145698',Stz=210,Spp=3}
[3] {Id='04239',Color='KB',MatPre='145698',Stz=210,Spp=2}
[4] {Id='04239',Color='03',MatPre='145698',Stz=210,Spp=4}

Result or new list that I whant to get is list with v items:

[0] {Id='04239',Color='03',MatPre='145698',Stz=210,Spp=3}
[1] {Id='04239',Color='03',MatPre='145698',Stz=210,Spp=4}
[2] {Id='04239',Color='KB',MatPre='145698',Stz=210,Spp=3}

I would need something like this

var test = list.GroupBy(x => new { x.Color })
               .Where(x => x.Spp is greater than first 
                                 smaller x.Spp in same 
                                 group of Color)
               .SelectMany(x => x).ToList();

I don't know how to do this.

This should do it, although calling Min every time might not be efficient, you might consider query syntax to make it better:

list.GroupBy(x => x.Color)
    .SelectMany(g => g.Where(x => x.Spp > g.Min(_ => _.Spp));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM