0

我在 linq 中有这样的查询

   var result = from item1 in context.ServicePriceInsertData
                                 join item2 in context.GroupMasterInsert
                                 on item1.G_Id equals item2.Group_Id
                                 join item3 in context.EmployeeServices
                                 on item1.Service_Id equals item3.E_ServiceId

                                 into dept
                                 from item4 in dept.DefaultIfEmpty()

                                 select new 
                                 {

                                     Service_Name = item1.S_Description,
                                     Group_Name = item2.Group_Name,
                                     ServiceId = item4.E_Id == null ? 1 : item4.E_Id

                                 };

现在我需要按 Service 和 Group_Name 来计算 ServiceId。请帮我?

4

1 回答 1

0

这未经测试,但请尝试.. 我希望它可以帮助您并了解如何在 linq 中分组和计数。更多细节linq + C# + group by + count

        var result = from item1 in context.ServicePriceInsertData
                     join item2 in context.GroupMasterInsert on item1.G_Id equals item2.Group_Id
                     join item3 in context.EmployeeServices on item1.Service_Id equals item3.E_ServiceId
                     group new {  item1.S_Description, item2.Group_Name, item1 } by new { item.S_Description } into g

                     select new
                     {

                         Service_Name = g.Key.S_Description,
                         Group_Name = g.Key.Group_Name,
                         Counted =  (int?)g.Count()
                     };
于 2012-12-21T05:02:32.763 回答