我有一个读取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
答案 0 :(得分:1)
代码在以下行失败:
averageDaysToCloseFormatted = String.Format("{0:0.##}",
x.Where(c => c.Closed_Flag == true)
.Average(c => c.Age))
如果没有设置Closed_Flag
的对象,则序列为空,无法计算Average
。解决问题的一种方法是将Age
转换为decimal?
(使其可为空),然后可空值的空序列的平均值为null
。然后我们使用null-coalescing运算符(??)将结果为null的平均值替换为0。
averageDaysToCloseFormatted = String.Format("{0:0.##}",
x.Where(c => c.Closed_Flag == true)
.Average(c => (decimal?)c.Age) ?? 0)