Given the following the following code.
class T {
public List<U> X { get; set; } /*.....*/
}
class U {
public int A { get; set; }
public int B { get; set; }
// other properties omit for easier testing
}
var l = new List<T> {
new T { X = new List<U> { new U { A = 0, B = 9 }, new U { A = 1, B = 8 } } },
new T { X = new List<U> { new U { A = 0, B = 9 }, new U { A = 1, B = 8 } } },
new T { X = new List<U> { new U { A = 2, B = 4 }, new U { A = 3, B = 5 } } },
new T { X = new List<U> { new U { A = 2, B = 4 }, new U { A = 3, B = 5 } } },
// ......
};
What's the most concise way to get the distinct List<T>
of the l
? The return is expected to have two sub-lists which X
has [{0,9}, {1,8}] and [{2,4}, {3,5}].
Updated code based on comments to Enigmativity's answer:
interface IBase<I> { I A { get; set; } I B { get; set; } }
class T<I> {
public List<U<I>> X { get; set; } /*.....*/
}
class U<I> : IBase<I> {
public I A { get; set; }
public I B { get; set; }
// other properties omit for easier testing
}
var l = new List<T<int>> {
new T<int> { X = new List<U<int>> { new U<int> { A=0, B=9 }, new U<int> { A=1, B=8 } } },
new T<int> { X = new List<U<int>> { new U<int> { A=0, B=9 }, new U<int> { A=1, B=8 } } },
new T<int> { X = new List<U<int>> { new U<int> { A=2, B=4 }, new U<int> { A=3, B=5 } } },
new T<int> { X = new List<U<int>> { new U<int> { A=2, B=4 }, new U<int> { A=3, B=5 } } },
// ......
};
Updated sample data as per comments:
var l = new List<T> {
new T { X = new List<U> { new U { A = 0, B = 9 }, new U { A = 1, B = 8 } } },
new T { X = new List<U> { new U { A = 0, B = 9 }, new U { A = 1, B = 8 } } },
new T { X = new List<U> { new U { A = 2, B = 4 }, new U { A = 3, B = 5 } } },
new T { X = new List<U> { new U { A = 2, B = 4 }, new U { A = 3, B = 5 } } },
new T { X = new List<U> {} }
// ......
};
>` and pass that in to a LINQ `.Distinct(IEqualityComparer)` call.
– Scott Chamberlain Feb 22 '16 at 22:54