[英]How to use GroupBy and Select in list using c#?
我有一個讀取XML頁面的腳本,將一些內容存儲到列表中。
列出列表后,我想按名稱對結果進行分組。
這是我所做的
DashboardViewModel vm = new DashboardViewModel();
using (var client = new WebClient())
{
string result = client.DownloadString(url);
XDocument doc = XDocument.Parse(result);
foreach (XElement row in doc.Descendants("row"))
{
var ticket = new Ticket();
ticket.company_name = row.Element("company_name").Value;
ticket.team_name = row.Element("team_name").Value;
ticket.Age = Parser.decimalVal(row.Element("Age").Value);
ticket.resource_list = row.Element("resource_list").Value;
ticket.Closed_Flag = Parser.boolVal(row.Element("Closed_Flag").Value);
ticket.Date_Entered_UTC = Parser.dateTimeVal(row.Element("Date_Entered_UTC").Value);
ticket.Date_Closed_UTC = Parser.dateTimeVal(row.Element("Date_Closed_UTC").Value);
tickets.Add(ticket);
}
}
vm.summaryByTeam = tickets.GroupBy(x => x.team_name)
.Select(x =>
new DashboardTicketTotal()
{
category = x.Key,
totalTickets = x.Count(),
totalOpenTickets = x.Where(c => c.Closed_Flag == false).Count(),
averageOpenDaysFormatted = String.Format("{0:0.##}", x.Average(c => c.Age)),
averageDaysToCloseFormatted = String.Format("{0:0.##}", x.Where(c => c.Closed_Flag == true).Average(c => c.Age)),
totalClosedInPast30Days = x.Where(c => c.Closed_Flag == true && c.Date_Closed_UTC > now.AddDays(-30)).Count()
}).ToList();
這是我的DashboardViewModel類
public class DashboardViewModel
{
public int totalOpen { get; set; }
public int totalClosed { get; set; }
public int over30DaysAge { get; set; }
public decimal? totalAge { get; set; }
public int totalTickets { get; set; }
public decimal? averageAge { get; set; }
public string averageAgeFormatted { get; set; }
public decimal? averageOpenAge { get; set; }
public decimal? totalOpenAge { get; set; }
public int totalClosedIn30Days { get; set; }
public int totalOpenTicketsOver30Day { get; set; }
public int totalCloseTicketsOver30Day { get; set; }
public int totalTicketOver30DaysOld { get; set; }
public int createdInPast30Days { get; set; }
public int age0To15 { get; set; }
public int age16To30 { get; set; }
public int age31To60 { get; set; }
public int age61To90 { get; set; }
public int age91Plus { get; set; }
public List<DashboardTicketTotal> summaryByTeam { get; set; }
public List<DashboardTicketTotal> summaryByCenter { get; set; }
public DashboardViewModel()
{
over30DaysAge = 0;
totalAge = 0;
totalTickets = 0;
createdInPast30Days = 0;
averageOpenAge = 0;
summaryByTeam = new List<DashboardTicketTotal>();
summaryByCenter = new List<DashboardTicketTotal>();
}
}
但是線
.Select(x =>
new DashboardTicketTotal()
給我一個錯誤
Sequence contains no elements
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains no elements
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.Average(IEnumerable`1 source) +2578534
System.Linq.Enumerable.Average(IEnumerable`1 source, Func`2 selector) +58
ConnectWiseReports.Controllers.<>c__DisplayClass30.<DashBoard>b__13(IGrouping`2 x) in c:....ReportsController.cs:102
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +157
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
ConnectWiseReports.Controllers.ConnectWiseReportsController.DashBoard() in c:...ReportsController.cs:100
lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +156
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651516
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
代碼在以下行中失敗:
averageDaysToCloseFormatted = String.Format("{0:0.##}",
x.Where(c => c.Closed_Flag == true)
.Average(c => c.Age))
如果沒有設置Closed_Flag
對象,則序列為空,無法計算Average
。 解決問題的一種方法是將Age
為decimal?
(使其可為空),則可為空值的空序列的平均值將為null
。 然后,我們使用空合並運算符(??)將所得的空平均值替換為0。
averageDaysToCloseFormatted = String.Format("{0:0.##}",
x.Where(c => c.Closed_Flag == true)
.Average(c => (decimal?)c.Age) ?? 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.