I have a class like this:
class MyClass<T> {
public string value1 { get; set; }
public T objT { get; set; }
}
and a list of this class. I would like to use .net 3.5 lambda or linq to get a list of MyClass by distinct value1. I guess this is possible and much simpler than the way in .net 2.0 to cache a list like this:
List<MyClass<T>> list;
...
List<MyClass<T>> listDistinct = new List<MyClass<T>>();
foreach (MyClass<T> instance in list)
{
// some code to check if listDistinct does contain obj with intance.Value1
// then listDistinct.Add(instance);
}
What is the lambda or LINQ way to do it?
Best Answer
Both Marc's and dahlbyk's answers seem to work very well. I have a much simpler solution though. Instead of using
Distinct
, you can useGroupBy
. It goes like this:Notice that I've passed two functions to the
GroupBy()
. The first is a key selector. The second gets only one item from each group. From your question, I assumedFirst()
was the right one. You can write a different one, if you want to. You can tryLast()
to see what I mean.I ran a test with the following input:
The result was:
I haven't tested it for performance. I believe that this solution is probably a little bit slower than one that uses
Distinct
. Despite this disadvantage, there are two great advantages: simplicity and flexibility. Usually, it's better to favor simplicity over optimization, but it really depends on the problem you're trying to solve.