I have the following Linq statement with ‘Group by’ clause and would like to know how to introduce a let or any other statement to avoid repeating the sub query, lifecycleEvents.Where(i => i.LifecycleEventId == grouping.Key).First() in the following example
var completionTimeModels =
from timeline in processTimelines
group timeline by timeline.LifecycleEventId into grouping
select new CompletionTimeViewModel()
{
// How to avoid repeating the same query to find the life cycle event?
Name = lifecycleEvents.Where(i => i.LifecycleEventId == grouping.Key).First().LifecycleEventName,
DisplayName = lifecycleEvents.Where(i => i.LifecycleEventId == grouping.Key).First().LifecycleEventDisplayName
};
1 Answer