obtenet information field in select distinct sum Linq

0

I have a list of elements as follows

    public class MedicamentoDevolverDto
{
    public int IdMedicamento { get; set; }
    public string CodigoMedicamento { get; set; }
    public string Medicamento { get; set; }
    public int StockActualCompartimento { get; set; }
    public string Color { get; set; }
    public int DispensadorId { get; set; }
    public int DispensadorBandejaId { get; set; }
    public int DispensadorCajonId { get; set; }
    public int CarroCajonId { get; set; }
    public bool MedicamentoDevolverOK { get; set; }
    public decimal CantidadDevolver { get; set; }
    public bool Devolver { get; set; }
    public string Estado { get; set; }
}

the quantity field, needs to be added, and I must generate a new list, grouped by Drug Id. For this I use the following sentence

                    var DistinctSumMedicamentos = lstMedicamentosDevolver
                    .GroupBy(l => l.IdMedicamento)
                    .Select(la =>
                        new
                        {
                            MedicamentoId = la.Key,
                            NoArticulos = la.Count(),
                            SumaCantidad = la.Sum(s => s.CantidadDevolver),

                        }).ToList();

My problem is, I require that in this new DistinctSumMedicamentos list I also see the name of the medication (Medication).

Look in the select to put the name of the medicine, but I can not find the sentence that allows me to add it.

Can someone help me with this doubt? Greetings and thanks for your time

    
asked by Luis Gabriel Fabres 10.03.2018 в 16:05
source

1 answer

0

You have to group also by the name of the medicine. try the following

var DistinctSumMedicamentos = lstMedicamentosDevolver
    .GroupBy(l => (l.IdMedicamento, l.Medicamento))
        .Select(la =>
            new
            {
                MedicamentoId = la.Key.IdMedicamento,
                Medicamento = la.Key.Medicamento,
                NoArticulos = la.Count(),
                SumaCantidad = la.Sum(s => s.CantidadDevolver)
             }).ToList();
    
answered by 10.03.2018 в 16:25