c# - Select from array only the best items -
i strugling come linq statement want select items in array have highest version. given array of apps, example:
[{name="adobe", version=1}, {name="adobe", version=2}, {name="adobe", version=3}, {name="vlc", version=1}, {name="vlc", version=2}, {name="vlc", version=3}]
i need linq select 1 adobe app, 1 highest version, , 1 vlc app. result should like:
[{name="adobe", version=3}, {name="vlc", version=3}]
the best got far is:
var names = apps.select((n => n.name)).distinct(); foreach (var name in names) { var latest = apps.where(n => n.name == name).orderbydescending(n => n.version).firstordefault(); //add latest new applist }
i keep on thinking whether there simpler, linq way this. suggestions?
thank you.
here simpler approach:
var latest = apps.groupby(i => i.name) .select(i => i.orderbydescending(j => j.version).first());
Comments
Post a Comment