由linq组成的长连接

时间:2013-12-02 13:06:01

标签: c# linq lambda

我试图在这个方法中使用多个连接来制作一个goup,但它没有给我任何回复

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;

我试图通过v.No进行分组,这样做。

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

这不会让我失望。在我的网格上,我是新手

1 个答案:

答案 0 :(得分:0)

将您的方法定义为(返回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;
}

然后将数据分组并将其作为匿名类型投影为:

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

现在,如果您的lib允许匿名类型序列化,则可以使用groupedData序列化为JSON。否则,您必须根据投影类型定义类型并使用它。