Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 带条件聚合的Viewmodel(总和)_Asp.net Mvc_Linq_C# 4.0 - Fatal编程技术网

Asp.net mvc 带条件聚合的Viewmodel(总和)

Asp.net mvc 带条件聚合的Viewmodel(总和),asp.net-mvc,linq,c#-4.0,Asp.net Mvc,Linq,C# 4.0,我有一个模型 public class RecTotal { public string PayType { get; set; } public decimal TotalPrice { get; set; } public string Category { get; set; } } public class ReconcileViewModel { public IEnumerable<RecTotal> RecTotals { get;

我有一个模型

public class RecTotal
{
    public string PayType { get; set; }

    public decimal TotalPrice { get; set; }

    public string Category { get; set; }
}

public class ReconcileViewModel
{
    public IEnumerable<RecTotal> RecTotals { get; set; }

    public IEnumerable<RecInvoiceLineItem> LineItems { get; set; }
}
我想要的最终结果是

Add(总和(总价),foreach(类别)


我知道我很接近,但我就是不能完全理解。

我找到了一种方法来做到这一点,但在我看来这是“丑陋的”,必须有更好的方法来做到这一点

var test = new List<RecTotal>();
            foreach (POSItemCategory cat in db.POSItemCategories)
            {
                test.Add(new RecTotal
                {
                    NumberOfItems = recLineItems.Where(p => p.Category == cat.ID).Count(),
                    TotalPrice = recLineItems.Where(p => p.Category == cat.ID).Sum(p => p.InvPrice),
                    PayType = "All",
                    Category = cat.Name,
                    NumberOfInvoices = recLineItems.Where(p => p.Category == cat.ID).Select(p => p.InvID).Distinct().Count()
                });
            };
            VM.RecTotals = test;
var测试=新列表();
foreach(数据库中的POSItemCategories类别类别POSItemCategories)
{
测试。添加(新的直肠总数)
{
NumberOfItems=recLineItems.Where(p=>p.Category==cat.ID).Count(),
TotalPrice=recLineItems.Where(p=>p.Category==cat.ID).Sum(p=>p.InvPrice),
PayType=“全部”,
类别=类别名称,
NumberOfInvoices=recLineItems.Where(p=>p.Category==cat.ID).选择(p=>p.InvID).Distinct().Count()
});
};
VM.RecTotals=测试;
如果有更好的方法,请告诉我。

你可以试试这个

   var testdata = new List<RecTotal>();

   testdata = recLineItems.Where(x => db.POSItemCategories.Select(a=>a.ID).Contains(x.Category))
                        .GroupBy(x => x.Category)
                        .Select(
                            x =>
                                new RecTotal()
                                {
                                    Category = x.Key,
                                    PayType = "All",
                                    TotalPrice = x.Sum(z => z.InvPrice),
                                    NumberOfItems = x.Count(),
                                    NumberOfInvoices = x.Select(p => p.InvID).Distinct().Count()
                                }).ToList();
var testdata=newlist();
testdata=recLineItems.Where(x=>db.POSItemCategories.Select(a=>a.ID.Contains(x.Category))
.GroupBy(x=>x.Category)
.选择(
x=>
新直肠总动脉()
{
类别=x.键,
PayType=“全部”,
TotalPrice=x.Sum(z=>z.InvPrice),
NumberOfItems=x.Count(),
NumberOfInvoices=x.Select(p=>p.InvID).Distinct().Count()
}).ToList();

cat.ID是一个字符串?类别名称?下面是对您有效的解决方案?我使用列表解决方案。我对您提出的解决方案有问题,而不是疑难解答,我只是使用了列表解决方案。谢谢您的建议。但是,我很难找到您提出的解决方案来解决如何制作羔羊肉的问题das工作正常,完全符合我的目的。
   var testdata = new List<RecTotal>();

   testdata = recLineItems.Where(x => db.POSItemCategories.Select(a=>a.ID).Contains(x.Category))
                        .GroupBy(x => x.Category)
                        .Select(
                            x =>
                                new RecTotal()
                                {
                                    Category = x.Key,
                                    PayType = "All",
                                    TotalPrice = x.Sum(z => z.InvPrice),
                                    NumberOfItems = x.Count(),
                                    NumberOfInvoices = x.Select(p => p.InvID).Distinct().Count()
                                }).ToList();