c# - removing duplicates in a list with linq -
suppose have list of myobject this:
public class myobject { public int objectid {get;set;} public string prop1 {get;set;} }
how remove duplicates list there multiple instance of objects same objectid.
thanks.
you can use groupby()
, select first item of each group achieve want - assuming want pick 1 item each distinct objectid
property:
var distinctlist = mylist.groupby(x => x.objectid) .select(g => g.first()) .tolist();
alternatively there distinctby()
in morelinq project allow more concise syntax (but add dependency project):
var distinctlist = mylist.distinctby( x => x.objectid).tolist();
Comments
Post a Comment