Pregunta

I have a query that needs to retrieve 3 fields:

| MaintenanceID | MaintenanceIDCount | StatusID |
|       1       |        2           |    -1    |
|       3       |        2           |    -1    |

The field MaintenanceIDCount (like the name says), is the count of MaintenanceID column.

My basic query expression is above:

var result = from m in Maintenance
     select new
     {
    m.MaintenanceID,
    m.StatusID
     }

The result of this query is:

| MaintenanceID | StatusID |
|       1       |    -1    |
|       1       |    -1    |
|       3       |    -1    |
|       3       |    -1    |

How can I group and mount my query to retrieve a column with the MaintenanceID column count?

Some tips?

¿Fue útil?

Solución

from m in Maintenance
group m by new { m.MaintenanceID, m.StatusID } into g
select new {
    g.Key.MaintenanceID,
    g.Key.StatusID,
    MaintenanceIDCount = g.Count()
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top