
Untitled
By: a guest on
May 1st, 2012 | syntax:
None | size: 0.88 KB | hits: 12 | expires: Never
Use Linq to construct distinct, ordered of objects based on properties
class Foo()
{
public Bar [] bars;
}
class Bar ()
{
public string name;
public int id;
}
foo[0] => bars { { name = "one", id = 1 }, { name = "two", id = 2 }, { name = "three", id = 3 },
foo[1] => bars { { name = "four", id = 4 }, { name = "four", id = 4 }
{ name = "one", id = 1 }, { name = "two", id = 2 }, { name = "three", id = 3 }, { name = "four", id = 4 }
var values = foo.SelectMany(f => f.bars)
.Distinct()
.OrderBy(b => b.id);
var values = foo.SelectMany(f => f.bars)
.DistinctBy(b => b.id)
.OrderBy(b => b.id);
var values = foo.SelectMany(f => f.bars)
.DistinctBy(b => new { b.id, b.name })
.OrderBy(b => b.id);
Foos.SelectMany(foo => foo.bars).OrderBy(bar => bar.id).Distinct()