Add two columns in LinQ

0

I am trying to add two columns from a class, but I am manipulating them with LinQ I have understood that you can add but I can not find you, my code is the following, I would greatly appreciate your help.

var Query = (from c in Conceptos.TablaConceptos
    select new TablaConcepto{
      ID = c.ID,
      FECHA = c.FECHA,
      REGION = c.REGION,
      ACTIVO = c.ACTIVO,
      INSTALACION = c.INSTALACION,
      TOTAL = c.TOTAL,
      EMPAQUE = c.EMPAQUE,
      QUEMADO = c.QUEMADO,
      INYECTADO = c.INYECTADO,
      ENDULZADO = c.ENDULZADO,
      AUTOCONSUMO = c.AUTOCONSUMO
  }).ToList();

return Query;'pudieran ayudar
    
asked by Pet 27.09.2018 в 01:19
source

2 answers

0

I hope I have understood your request. With the following query you get, in TablaConcepto.Total the sum of the PACKAGING, BURNED, INJECTED fields, (you can add or remove). That is, you get the total of each Item within the TOTAL field, in the answer of @Aron Romero it explains how to make the sum of all the elements that the query will return.

var Query = (from c in Conceptos.TablaConceptos
             select new TablaConcepto
             {
                 ID = c.ID,
                 FECHA = c.FECHA,
                 REGION = c.REGION,
                 ACTIVO = c.ACTIVO,
                 INSTALACION = c.INSTALACION,
                 TOTAL = c.TOTAL + c.EMPAQUE + c.QUEMADO + c.INYECTADO,
                 EMPAQUE = c.EMPAQUE,
                 QUEMADO = c.QUEMADO,
                 INYECTADO = c.INYECTADO,
                 ENDULZADO = c.ENDULZADO,
                 AUTOCONSUMO = c.AUTOCONSUMO
             }).ToList();

            return Query;
    
answered by 27.09.2018 в 01:44
0

How are you returning a list you can use the SUM () function.
You do not exactly want to add, but you can apply for a single property.

var sumTotal = Query.Sum(s => s.TOTAL);

Or you can do it with several in the same amount

var sumTotalConsumo  = Query.Sum(s => s.TOTAL + s.AUTOCONSUMO);

But if what you are looking for is for each record, add it from the time you inquire.

var Query = (from c in Conceptos.TablaConceptos
                         select new TablaConcepto
                         {

                             ID = c.ID,
                             FECHA = c.FECHA,
                             REGION = c.REGION,
                             ACTIVO = c.ACTIVO,
                             INSTALACION = c.INSTALACION,
                             TOTAL = x.EMPAQUE + x.INYECTADO + x.ENDULZADO + x.AUTOCONSUMO,
                             EMPAQUE = c.EMPAQUE,
                             QUEMADO = c.QUEMADO,
                             INYECTADO = c.INYECTADO,
                             ENDULZADO = c.ENDULZADO,
                             AUTOCONSUMO = c.AUTOCONSUMO


                         }).ToList();
    
answered by 27.09.2018 в 01:23