gpt4 book ai didi

c# - 如何使用 Group by 在 Linq 语句中引入 Let 关键字

转载 作者:太空狗 更新时间:2023-10-29 20:03:40 25 4
gpt4 key购买 nike

我有以下带有“Group by”子句的 Linq 语句,想知道如何引入 let 或任何其他语句以避免重复子查询,lifecycleEvents.Where(i => i.LifecycleEventId = = grouping.Key).First() 在下面的例子中

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
};

关于c# - 如何使用 Group by 在 Linq 语句中引入 Let 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5596899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com