From 1debac950598d755d0f9bb4b1982c497fdd4e546 Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Thu, 2 Sep 2021 10:13:20 +0500 Subject: [PATCH] Fixed exception with .ElementAt(0) call at empty collection --- .../Services/WellOperationsStatService.cs | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs index 0798c683..f670e045 100644 --- a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs @@ -470,24 +470,30 @@ namespace AsbCloudInfrastructure.Services private static IEnumerable MakeOperationsExt(IEnumerable operations) { - var sortedOperations = operations.OrderBy(o => o.StartDate); - var count = operations.Count(); - var ops = new List(count); - var item = operations.ElementAt(0); - var wellDepth = item.WellDepth; - var pre = new OperationParams(item); - var current = new OperationParams(item); - for (int i = 1; i < count; i++) + var ops = new List(); + + if (operations.Any()) { - item = operations.ElementAt(i); - current = new OperationParams(item){ WellDepth = Helper.Max(wellDepth, item.WellDepth) }; - pre.DeltaDepth = current.WellDepth - wellDepth; - wellDepth = current.WellDepth; - pre.Hours = (current.Start - pre.Start).TotalHours; - ops.Add(pre); - pre = current; + var sortedOperations = operations.OrderBy(o => o.StartDate); + var count = operations.Count(); + ops = new List(count); + var item = operations.ElementAt(0); + var wellDepth = item.WellDepth; + var pre = new OperationParams(item); + var current = new OperationParams(item); + for (int i = 1; i < count; i++) + { + item = operations.ElementAt(i); + current = new OperationParams(item) { WellDepth = Helper.Max(wellDepth, item.WellDepth) }; + pre.DeltaDepth = current.WellDepth - wellDepth; + wellDepth = current.WellDepth; + pre.Hours = (current.Start - pre.Start).TotalHours; + ops.Add(pre); + pre = current; + } + ops.Add(current); } - ops.Add(current); + return ops; } }