从两个表中获取Id的所有实例的总和

时间:2018-03-20 03:43:31

标签: asp.net-mvc linq group-by ienumerable

我想在QualificationBatch中显示CompendiumId的所有实例的Sum.Slots,同样,在SAP中显示CompendiumId的所有实例的所有Sum.Seats,并将其显示在如下的表中:

Compendium | Sum.Seats | Sum.Slots

Id1 | Seats1 | Slots1

Id2 | Seats2 | Slots2

经过大量阅读后,我认为使用GroupBy可以做到这一点,但我无法让它工作,因为我对所有内容都相当新,即使是操作List或IEnumerable等基础知识。这就是我到目前为止所做的:

模型

public class Compendium
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<QualificationBatch> QualificationBatches { get; set; }        
    public virtual ICollection<SAP> SAPs { get; set; }
}


public class QualificationBatch
{
    public int Id { get; set; }
    public int CompendiumId { get; set; }
    public int Slots { get; set; }

    public virtual Compendium Compendium { get; set; }
}

public class SAP
{
    public int Id { get; set; }
    public int CompendiumId { get; set; }
    public int Seats { get; set; }

    public virtual Compendium Compendium { get; set; }
}

视图模型

public class SAPSummaryViewModel
    {        
        public int Id { get; set; } //Compendium
        public int Slots { get; set; } //QualificationBatch
        public int Seats { get; set; } //SAP
    }

控制器

public ActionResult Index()
        {
            List<SAPSummaryViewModel> SAPSummaryViewModelList = new List<SAPSummaryViewModel>();
            var sapsummarylist = (from SP in db.SAPs
                                  join Comp in db.Compendia on SP.CompendiumId equals Comp.Id                                  
                                  select new {Comp.Id, SP.Seats });
            sapsummarylist.GroupBy(i => i.Id).Select(group =>
                                              new
                                              {
                                                  Id = group.Key,
                                                  Sum = group.Sum(item => item.Seats)
                                              });          
            foreach (var item in sapsummarylist)
            {                
                SAPSummaryViewModel objcvm = new SAPSummaryViewModel();              
                objcvm.Id = item.Id;
                objcvm.Seats = item.Seats;                
                SAPSummaryViewModelList.Add(objcvm);
            }
            return View(SAPSummaryViewModelList);
        }

我对其他方式持开放态度,我只是将上面控制器的代码放在上面,以显示我一直在尝试的内容。在这里,我试图让它首先在一张桌子上工作,但我已经卡在这里因为它不起作用。

2 个答案:

答案 0 :(得分:0)

以下代码对您有所帮助,

var sapsummarylist = ( from cmp in Compendium
                       join sp in SAP on cmp.Id equals sp.CompendiumId
                       join qb in QualificationBatch on  on cmp.Id equals qb.CompendiumId
                       group  new { cmp.Id , sp.Seats, qb.Slots } by new { cmp.Id } into mgrp           
                       from grp in mgrp.DefaultIfEmpty() 
                       select new SAPSummaryViewModel { 
                                Id = grp.Id,
                                Seats = mgrp.Sum(x=>x.Seats),
                                Slots = mgrp.Sum(x=>x.Slots)                                    
                        });  

答案 1 :(得分:0)

因此每个QualificationBatch都有属性CompendiumId和int属性Slots。

每个SAP都有一个属性CompendiumId。它还有一个int属性Seats。

现在显然你有QualificationBatches和SAP的序列,并且你想要每个compendiumId的插槽和座位的值

小步骤:

IQueryable<QualificationBatch> qualificationBatches = db.QualificationBatches;
IQueryable<SAP> saps = db.Saps;

var result = qualificationBatches    // join qualificationbatches
    .Join(saps,                      // with saps
    qualificationBatch => qualificationBatch.CompendiumId,
                                     // from every q-batch take the CompendiumId,
    sap => sap.CompendiumId,         // from every sap take the CompendiumId,
    (qualificationBatch, sap)  => new SapSummaryViewModel()
    {                                // when they match make a new SapSummaryViewModel
         Id = qualificationBatch.Id,
         Slots =  qualificationBatch.Slots,
         Seats = qualificationBatch.Seats,
     });

TODO:发表一个声明

请注意,这是一个内部联接:如果您有一个没有任何QualificationBatch使用的CompendiumId的SAP,您将无法在最终结果中获得它。如果您还想在最终结果中使用这些项目,则根据您的需要,您需要左外连接或完全外连接。 LINQ不支持它们,但你可以通过为它编写一个方法来扩展LINQ。见Stackoverflow LINQ full outer join