下面的 linq to sql query for problim 是什么?

What is linq to sql query for problim below?

我有一个 table,它有货件 ID 和客户 ID;我希望得到与每个客户 ID 关联的 shipmentid 计数作为结果。什么是 linq?

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? –

您可以尝试这样的操作:

var result = db.shipmentClient
               .GroupBy(sc=>sc.clientId)
               .Select(gr=>new 
               {
                   ClientId = gr.Key(),
                   Shipments = gr.Count()
               })
               .OrderByDescending(x=>x.Shipments)
               .Take(2);

我想 db 是您的数据库上下文 class 并且 shipmentClient 是您的 table 的名称。如果是这样,您可以根据 ClientId 像上面那样进行分组,然后统计与客户关联的发货数量。然后你订购结果,你 select 前两个结果。