.NET CORE 3.0 LeftJoin和GroupBy + 2

时间:2019-12-24 07:05:59

标签: asp.net-core

关于如何实现Left Join,GroupBy和2的任何建议?我使用.Net Core 3.0

ViewModel

public class SiswaVM
{
    public int Id { get; set; }
    public string NomorVA { get; set; }
    public string NamaSiswa { get; set; }
    public string Kelas { get; set; }
    public string Unit { get; set; }

    [DataType(DataType.Date)]
    public DateTime Tanggal { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal SPP { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal Catering { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal Infaq { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal DaftarUlang { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal UangPangkal { get; set; }

    public virtual List<SiswaVM> ListTagihan { get; set; }
    public virtual List<SiswaVM> ListTagihanLalu { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal TagihanBulanIni { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal TagihanBulanLalu { get; set; }
}

控制器

var data = await (from a in _context.Siswa
                    join b in _context.Pendapatan on a.NomorVA equals b.NomorVA into c
                    from b in c.DefaultIfEmpty()
                    select new SiswaVM
                    {
                        NomorVA = a.NomorVA,
                        Tanggal = b.Tanggal,
                        NamaSiswa = a.NamaSiswa,
                        Kelas = a.Kelas,
                        Unit = a.Unit,
                        SPP = b.SPP,
                        Catering = b.Catering,
                        DaftarUlang = b.DaftarUlang,
                        Id = a.Id,
                        Infaq = b.Infaq
                    }).ToListAsync();
var dataa = (from a in data
                group a by a.NomorVA into grp
                join b in _context.Siswa on grp.First().NomorVA equals b.NomorVA
                select new SiswaVM
                {
                    NomorVA = grp.Key,
                    Tanggal = grp.First().Tanggal,
                    NamaSiswa = b.NamaSiswa,
                    Kelas = b.Kelas,
                    Unit = b.Unit,
                    ListTagihan = grp.ToList()
                }).ToList();
return View(dataa);

该代码传递给我的视图,这里是我的视图代码

<tbody>
    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.NomorVA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NamaSiswa)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Kelas)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Unit)
        </td>
        @foreach (var items in from a in item.ListTagihan where a.Tanggal.Month < DateTime.Now.Month group a by a.NomorVA into grp select grp)
        {
            <td>
                @items.Sum(x => x.SPP))
            </td>
        }
        @foreach (var items in from a in item.ListTagihan where a.Tanggal.Month == DateTime.Now.Month group a by a.NomorVA into grp select grp)
        {
            <td>
                @items.Sum(x => x.SPP))
            </td>
        }
    </tr>
    }
</tbody>

使用此代码,我成功执行了左联接,但是在视图foreach的视图表中,上个月和本月不是我期望的。 而这是我从代码中得到的结果,请参阅groupby +,其中本月未显示零(0),就像groupby +上个月的地方

enter image description here

0 个答案:

没有答案