3

it looks fairly simple but I can not get my mind over it.

I have a list:

List<IGrouping<byte, MyClass>>

MyClass object has a timestamp property, and I also have list of timestamp, now I want to know if there is elegant way to get all values from grouping list where Timestamp property is in my timestamp list?

I have solved the problem, but i think it can be solved in more efficient way. code would look like:

var loadedValues = new List<MyClass>();
foreach (IGrouping<byte, MyClass> value in Values)
{
    loadedValues.AddRange(value.Select(c => c).Where(c => Timestamps.Any(point => c.Timestamp == point)));
}
Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87

1 Answers1

0

You could use Join which uses a set based approach, hence it's more efficient than Where:

IEnumerable<MyClass> query = 
                from grp in Values
                from cls in grp
                join ts in Timestamps on cls.Timestamp equals ts
                select cls;

List<MyClass> loadedValues = query.ToList();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939