Если у вас есть List<Result>
, называемый results
, то вот ключевая часть, которую вам нужно сделать:
from result in results
from action in result.Actions
group action by action.Description into actions
Вот мой полный код для решения вашей проблемы:
var results = new List<Result>()
{
new Result()
{
Description = "Main",
StartTime = new DateTime(2020, 3, 12, 11, 9, 31),
Actions = new List<Result>()
{
new Result() { Description = "ValidateUser", Elapsed = TimeSpan.FromSeconds(0.95), },
new Result() { Description = "GetInvoiceList", Elapsed = TimeSpan.FromSeconds(12.73), },
new Result() { Description = "SearchFolio", Elapsed = TimeSpan.FromSeconds(4.17), },
new Result() { Description = "SearchPatients", Elapsed = TimeSpan.FromSeconds(6.12), },
new Result() { Description = "SearchOrganization", Elapsed = TimeSpan.FromSeconds(8.6), },
}
},
new Result()
{
Description = "Main",
StartTime = new DateTime(2020, 3, 12, 11, 9, 31),
Actions = new List<Result>()
{
new Result() { Description = "ValidateUser", Elapsed = TimeSpan.FromSeconds(0.49), },
new Result() { Description = "SearchFolio", Elapsed = TimeSpan.FromSeconds(3.69), },
new Result() { Description = "GetInvoiceList", Elapsed = TimeSpan.FromSeconds(10.15), },
new Result() { Description = "SearchOrganization", Elapsed = TimeSpan.FromSeconds(0.55), },
new Result() { Description = "SearchPatients", Elapsed = TimeSpan.FromSeconds(6.91), },
}
},
new Result()
{
Description = "Main",
StartTime = new DateTime(2020, 3, 12, 11, 9, 31),
Actions = new List<Result>()
{
new Result() { Description = "ValidateUser", Elapsed = TimeSpan.FromSeconds(0.84), },
new Result() { Description = "SearchFolio", Elapsed = TimeSpan.FromSeconds(7.29), },
new Result() { Description = "SearchOrganization", Elapsed = TimeSpan.FromSeconds(5.99), },
new Result() { Description = "SearchPatients", Elapsed = TimeSpan.FromSeconds(7.70), },
new Result() { Description = "GetInvoiceList", Elapsed = TimeSpan.FromSeconds(10.45), },
}
},
};
string ElaspedTime(TimeSpan elapsed) => $"{elapsed.Hours:00}:{elapsed.Minutes:00}:{elapsed.Seconds:00}.{(elapsed.Milliseconds / 10):00}";
var output1 =
String.Join(
Environment.NewLine + Environment.NewLine,
from result in results
let header = new []
{
result.Description,
result.StartTime.ToString(),
}
let details =
result
.Actions
.SelectMany(action => new[]
{
"-----------------",
action.Description,
ElaspedTime(action.Elapsed)
})
.Concat(new[] { "-----------------" })
.Select(x => $" {x}")
let tail = new[]
{
ElaspedTime(result.Actions.Select(x => x.Elapsed).Aggregate((x, y) => x.Add(y))),
}
select String.Join(Environment.NewLine, header.Concat(details).Concat(tail)));
var output2 =
String.Join(
Environment.NewLine + Environment.NewLine,
from result in results
from action in result.Actions
group action by action.Description into actions
let details =
actions
.SelectMany(action => new[]
{
"-----------------",
action.Description,
ElaspedTime(action.Elapsed)
})
.Concat(new[] { "-----------------" })
.Select(x => $" {x}")
select String.Join(Environment.NewLine, details));
Для output1
Я получаю:
Main
2020/03/12 11:09:31
-----------------
ValidateUser
00:00:00.95
-----------------
GetInvoiceList
00:00:12.73
-----------------
SearchFolio
00:00:04.17
-----------------
SearchPatients
00:00:06.12
-----------------
SearchOrganization
00:00:08.60
-----------------
00:00:32.57
Main
2020/03/12 11:09:31
-----------------
ValidateUser
00:00:00.49
-----------------
SearchFolio
00:00:03.69
-----------------
GetInvoiceList
00:00:10.15
-----------------
SearchOrganization
00:00:00.55
-----------------
SearchPatients
00:00:06.91
-----------------
00:00:21.79
Main
2020/03/12 11:09:31
-----------------
ValidateUser
00:00:00.84
-----------------
SearchFolio
00:00:07.29
-----------------
SearchOrganization
00:00:05.99
-----------------
SearchPatients
00:00:07.70
-----------------
GetInvoiceList
00:00:10.45
-----------------
00:00:32.27
Для output2
Я получаю:
-----------------
ValidateUser
00:00:00.95
-----------------
ValidateUser
00:00:00.49
-----------------
ValidateUser
00:00:00.84
-----------------
-----------------
GetInvoiceList
00:00:12.73
-----------------
GetInvoiceList
00:00:10.15
-----------------
GetInvoiceList
00:00:10.45
-----------------
-----------------
SearchFolio
00:00:04.17
-----------------
SearchFolio
00:00:03.69
-----------------
SearchFolio
00:00:07.29
-----------------
-----------------
SearchPatients
00:00:06.12
-----------------
SearchPatients
00:00:06.91
-----------------
SearchPatients
00:00:07.70
-----------------
-----------------
SearchOrganization
00:00:08.60
-----------------
SearchOrganization
00:00:00.55
-----------------
SearchOrganization
00:00:05.99
-----------------