简体   繁体   中英

Group by linq with a long join

Im trying to make a goup by with a multiple join in this method, but it return me nothing

public List<CTarifaAplicada> get_TarifasAplicadas(int id_proforma)
        {
            var pt = (from ta in db.TarifaAplicada
                      where ta.IDProforma == id_proforma
                      join p in db.Proforma on ta.IDProforma equals p.ID
                      join pe in db.PortExpenses on ta.CodigoPE equals pe.CodigoPE
                      join v in db.Voucher on pe.IDVoucher equals v.No
                      select new CTarifaAplicada
                      {
                          CodigoFile = ta.CodigoFile,
                          CodigoPE = ta.CodigoPE,
                          Fecha = ta.Fecha,
                          Id = ta.Id,
                          IDProforma = ta.IDProforma,
                          ITBIS = ta.ITBIS,
                          Monto = ta.Monto,
                          DWT = p.DWT,
                          GRT=p.GRT,
                          LOA=p.LOA,
                          no_Voucher= v.No,
                          voucher=v.Description
                      }).ToList();

            return pt;

Im trying to group by v.No , by doing this.

Json(cta.get_TarifasAplicadas(id_Proforma).OrderBy(n => n.no_Voucher).GroupBy(n => n.no_Voucher)

And this return me nothing. on my grid, Im newbie

Define your method as (returning Enumerable instead of List is more sensible):

public IEnumerable<CTarifaAplicada> get_TarifasAplicadas(int id_proforma)
{
    var pt = (from ta in db.TarifaAplicada
                where ta.IDProforma == id_proforma
                join p in db.Proforma on ta.IDProforma equals p.ID
                join pe in db.PortExpenses on ta.CodigoPE equals pe.CodigoPE
                join v in db.Voucher on pe.IDVoucher equals v.No
                select new CTarifaAplicada
                    {
                        CodigoFile = ta.CodigoFile,
                        CodigoPE = ta.CodigoPE,
                        Fecha = ta.Fecha,
                        Id = ta.Id,
                        IDProforma = ta.IDProforma,
                        ITBIS = ta.ITBIS,
                        Monto = ta.Monto,
                        DWT = p.DWT,
                        GRT=p.GRT,
                        LOA=p.LOA,
                        no_Voucher= v.No,
                        voucher=v.Description
                    });
    return pt;
}

Then group your data and project them as anonymous type as:

var groupedData = cta.get_TarifasAplicadas(id_Proforma)
    .GroupBy(n => new {n.no_Voucher, n.voucher, n.DWT, n.GRT, n.LOA, n.Fecha})
    .Select(g => new
                   {
                       g.Key.no_Voucher,
                       g.Key.voucher,
                       ITBIS = g.Sum(r => r.ITBIS),
                       MONTO = g.Sum(r => r.Monto),
                       g.Key.DWT,
                       g.Key.GRT,
                       g.Key.LOA,
                       g.Key.Fecha 
                   });

Now you can use groupedData to serialize as JSON if your lib allows anonymous type serialization. Otherwise, You must define type according to the projected type and use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM