I have some objects:
class Foo {
public Guid id;
public string description;
}
var list = new List<Foo>();
list.Add(new Foo() { id = Guid.Empty, description = "empty" });
list.Add(new Foo() { id = Guid.Empty, description = "empty" });
list.Add(new Foo() { id = Guid.NewGuid(), description = "notempty" });
list.Add(new Foo() { id = Guid.NewGuid(), description = "notempty2" });
I would like to process this list in such a way that the id
field is unique, and throw away the non-unique objects (based on id).
The best I could come up with is:
list = list.GroupBy(i => i.id).Select(g=>g.First()).ToList();
Is there a nicer/better/quicker way to achieve the same result.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…