本文介绍了如何介绍一下里面的Linq与集团发言let关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有集团通过条款如下LINQ的发言,并想知道如何引入一个让或任何其他语句,避免重复的子查询, lifecycleEvents.Where(I => i.LifecycleEventId == grouping.Key)。首先()
在下面的例子
VAR completionTimeModels =
从时间轴上processTimelines
组时间线timeline.LifecycleEventId到分组
选择新CompletionTimeViewModel()
{
//如何避免重复相同的查询,找到生命周期的事件吗?
名称= lifecycleEvents.Where(I => i.LifecycleEventId == grouping.Key)。首先()LifecycleEventName,
显示名称= lifecycleEvents.Where(I => i.LifecycleEventId == grouping.Key)。首先()LifecycleEventDisplayName。
};
解决方案
VAR completionTimeModels =
从时间轴上processTimelines
组时间线timeline.LifecycleEventId到分组
让富= lifecycleEvents.First(I => i.LifecycleEventId == grouping.Key)
选择新CompletionTimeViewModel()
{
名称= foo.LifecycleEventName,
显示名称= foo.LifecycleEventDisplayName
};
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
};
解决方案
var completionTimeModels =
from timeline in processTimelines
group timeline by timeline.LifecycleEventId into grouping
let foo = lifecycleEvents.First(i => i.LifecycleEventId == grouping.Key)
select new CompletionTimeViewModel()
{
Name = foo.LifecycleEventName,
DisplayName = foo.LifecycleEventDisplayName
};
这篇关于如何介绍一下里面的Linq与集团发言let关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!