关于c#:linq group by和count

linq group by and count

1
2
3
4
5
6
select  uc.adminid, count(*)
from Users uc
join UsersMessage ucm on uc.admincallid = ucm.admincallid
where uc.CallDate between '2016-08-01' and '2016-09-01' and ucm.type = 4
group by uc.adminid
order by count(*)

我试过的是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 public static Dictionary<int, int> ReturnSomething(int month)
        {

            Dictionary<int, int> dict = new Dictionary<int, int>();
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                LinqMetaData meta = new LinqMetaData(adapter);
                dict = (from uc in meta.Users
                        join ucm in meta.meta.UsersMessage on uc.AdminCallId equals ucm.AdminCallId
                        where ucm.type == 4 && uc.CallDate.Month == month
                        group uc by uc.AdminId into g
                        select new { /* ???? adminid = g. */ }).ToDictionary(x => new Dictionary<int, int>(/* ????? x, x.Name*/));
            }

            return dict;
        }

我怎样才能达到我所需要的?


the key of the dictionary is the key of the GroupByand the value is the Count(),知道你需要:P></

1
2
// ...
.ToDictionary(g => g.Key, g => g.Count()); // key is AdminCallId and value how often this Id occured

因为我问你how to:恩降阶P></

你是不是在字典,which has建设阶(读:嗯,它应该是不确定性)。我知道is the序完全不必要的民族。为什么是无序的?read thisP></

但如果你能创造什么东西你想知道如何使用这Count DESC阶模式:你可以P></

1
2
3
4
5
6
from uc in meta.Users
join ucm in meta.meta.UsersMessage on uc.AdminCallId equals ucm.AdminCallId
where ucm.type == 4 && uc.CallDate.Month == month
group uc by uc.AdminId into g
orderby g.Count() descending
select new { adminid = g.Key, count = g.Count() })