简体   繁体   English

通过linq进行分组并加入很长时间

[英]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. 我试图通过这样做来对v.No进行分组。

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): 将您的方法定义为(更明智的是返回Enumerable而不是List ):

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. 现在,如果您的库允许匿名类型序列化,则可以使用groupedData序列化为JSON Otherwise, You must define type according to the projected type and use it. 否则,您必须根据投影类型定义类型并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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