Entity Framework: cuente registros con el mismo nombre

I have a customer table filled with customer detail and I would like to count all the records in that table and return how many records exist in the table for each name.

So if I have two customers with name Erik, and three records with name Roberts. The function would return two Eriks and three Robers.

preguntado el 08 de noviembre de 11 a las 19:11

2 Respuestas

You could use the group by part of linq for this

from grp in (
    from customer in customers
    group customer.Name by Customer.Name
 select new {Name = grp.Key, Count = grp.Count()};

That will give you an set of objects with a property "Name" and a property "Count" the count being how many customers with that particular name you have. Then you can use that information as needed

respondido 08 nov., 11:23

¿Qué tal esto?

Customers.GroupBy(x => x.Name)
    .Select(x => new { Name = x.Key, Count = x.Count() })

respondido 08 nov., 11:23

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.