Join Statement for Two Unrelated Entities to Get Distinct Sum

霸气de小男生 提交于 2020-01-05 04:59:06

问题


I need a lot of help with my join statement, as it multiplies the rows of the two tables that I'm trying to join:

My Sample data:

SAPId CompendiumId Seats
----- ------------ -----
1     443          21
2     443          22
3     443          23
4     443          24
5     443          25
6     571          25
7     352          20

QBId  CompendiumId  Slots
----- ------------ -----
1     443          26
2     443          27
3     571          25
4     571          23

My desired output is:

CompendiumId Seats Slots
------------ ----- -----
443          115   53
571          25    48
352          20    0

but the result of my code is:

CompendiumId Seats Slots
------------ ----- -----
443          230   265
571          50    48

I think what happens here is this, where the cells highlighted with red are the ones that get duplicated:

Here is my code:

Controller

    private MyContext db = new MyContext();

    public ActionResult Index()
    {           
        var sapsummarylist = (from cmp in db.Compendia
                              join sp in db.SAPs on cmp.Id equals sp.CompendiumId
                              join qb in db.QualificationBatches 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)
                              });

        return View(sapsummarylist.Distinct());  
    }

Models

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; }
}

ViewModel

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

回答1:


Following code will be helpful to you,

var sapsummarylist = (from sp in Saps
                      group sp by new { sp.CompendiumId } into grp
                      select new SAPSummaryViewModel
                      {
                         Id = grp.FirstOrDefault().CompendiumId,
                         Seats = grp.Sum(x => x.Seats),
                         Slots = QualificationBatches.Where(x=>x.CompendiumId == grp.FirstOrDefault().CompendiumId).Sum(x => x.Slots)??0
                      });


来源:https://stackoverflow.com/questions/49379715/join-statement-for-two-unrelated-entities-to-get-distinct-sum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!