gpt4 book ai didi

c# - 从列表中的 linq 查询中选择特定字段

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:42 25 4
gpt4 key购买 nike

我需要优化我的 linq 查询,它看起来像这样:

List<List<Step>> steps = (from LoadTest l in monitoringTask.LoadTests
where (l.DateStart > (start - Core.session.timeSpanClientServer)
&& l.DateStart < (stop - Core.session.timeSpanClientServer))
orderby l.DateStart ascending
select (l.H_Scenario.Steps
.Where(x => x.IsActivInGlobalApdexCounting == true))
.ToList()).ToList();

为了优化它,我只想从我需要的类步骤中选择字段,所以我创建了类 StepDTO .

这是类 Step

 private class Step
{
private System.Int64 id;
private System.Int32 responseTime;
private H_Scenario h_Scenario;
/other fields*/
}

和类StepDTO

 private class StepDTO
{
private System.Int64 id;
private System.Int32 responseTime;
private H_Scenario h_Scenario;
}

问题是如何从查询中得到List<List<StepDTO>>通过创建 StepDTO选择子句中的实例而不是选择整个 l.H_Scenario.Steps .

最佳答案

您只需要使用Select 并在其中创建新的对象实例。给你:

List<List<Step>> steps = (from LoadTest l in monitoringTask.LoadTests
where (l.DateStart > (start - Core.session.timeSpanClientServer)
&& l.DateStart < (stop - Core.session.timeSpanClientServer))
orderby l.DateStart ascending
select (l.H_Scenario.Steps
.Where(x => x.IsActivInGlobalApdexCounting == true))
//Here is you creating your DTO
.Select(x => new StepDTO
{
id = x.id
responseTime = x.responseTime
h_Scenario = x.h_Scenario
})
.ToList()).ToList();

关于c# - 从列表中的 linq 查询中选择特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39276212/

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