Reputation: 37
I have a table which has shipment id and clientid; I want to have the count of shipmentid associated with each client id as result. What is linq for that?
Upvotes: 0
Views: 21
Reputation: 53958
The table consists of two fields ClientID and ShipID shipId is primary key and a clientID can have multiple shipid's .i want to have query that returns count of shipid's along with the associated clientid and select top two results from the result.how can i do that? –
You could try something like this:
var result = db.shipmentClient
.GroupBy(sc=>sc.clientId)
.Select(gr=>new
{
ClientId = gr.Key(),
Shipments = gr.Count()
})
.OrderByDescending(x=>x.Shipments)
.Take(2);
I suppose that db
is your database context class and shipmentClient is the name of your table. If so, you can make a group by like above based on the ClientId and then count how many shipments are associated with a client. Then your order the results and you select the top two results.
Upvotes: 1