简体   繁体   English

linq,分组并计数

[英]linq, group by and count

I have a list of Unions with several members and another table with page hits by Union. 我有一个有几个成员的工会列表,另一个有按工会点击的表格。 Need a report that list each union, the number of each type of member and the clicks (page views). 需要一个报告,列出每个联合,每种成员的数量以及点击次数(页面浏览量)。

clicks are on a separate table with a date and unionID 点击位于带有日期和unionID的单独表格上

this is close but doesn't really have the results I want. 这很接近,但实际上并没有我想要的结果。 The count shows the total of members but not the total of clicks. 该计数显示成员总数,但不显示点击总数。 How do i get the total clicks? 我如何获得总点击次数? (count records in clicks table that match the UnionID) (对点击表中与UnionID匹配的记录进行计数)

(from c in db.view_Members_Details
 join h in db.tbl_Clicks on c.unionID equals h.unionID 
 group c by new { c.UnionName, h.unionID } into g
 select new
 {
     TotalClicks = g.Count(),
     UnionName = g.Key.UnionName,
     userTypeID1 = g.Where(x => x.UnionName.Equals(g.Key.UnionName) && x.userTypeID.Equals(1)).Count(),
     userTypeID2= g.Where(x => x.UnionName.Equals(g.Key.UnionName) && x.userTypeID.Equals(2)).Count(),
     userTypeID3= g.Where(x => x.UnionName.Equals(g.Key.UnionName) && x.userTypeID.Equals(3)).Count(),
 }).ToList();

results should be: 结果应该是:

Clicks Count |  Union Name | userTypeID1 Count  | userTypeID2 Count  | userTypeID3 Count |

I don't think you need the first condition in your WHERE, because you're already grouping on the UnionName . 我认为您不需要WHERE中的第一个条件,因为您已经在UnionName上分组了。

g.Count(x => x.userTypeID == 3) //etc for 1, 2, 3

As for the clicks, try the following: 至于点击次数,请尝试以下操作:

TotalClicks = g.Count(x => x.unionID == g.Key.unionID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM