2016-09-02 8 views
0
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(*) 

をカウントし、ここで私が試したものです:LINQグループによると

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; 
     } 

どのように私は私が必要なものを達成することができますか?

+1

*どのように私は*私は必要なものを達成し、あなたの必要性は何ができますか?? –

+0

私のSQLクエリが返すものを返すには、 –

+0

に 'g.Key'と' g.Count() 'が必要です。 [この質問](http://stackoverflow.com/questions/7285714/linq-with-groupby-and-count)を見てください。 –

答えて

1

辞書のキーはGroupByの鍵であり、あなたが必要なので、値は、Count()です:

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

あなたはそれが降順注文する方法を求めてきましたので:

あなたがいます順序を持たない辞書を作成する(よく読んでください:非決定論的です)。したがって、注文は が完全に不要です。それはなぜ順序付けられていないのですか? Read this

しかし、あなたは何か他のものを作成し、あなたがこれを使用することができCount DESCで注文する方法を知りたい場合:

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() }) 
+0

うん、うまくいきます。 Btw、Count()で降順に並べ替える場合は、静的関数をどのように変更するのですか? –

+0

@FlorinM:命令を持たない辞書を構築しています(よく読んでください:それは非決定論的です)。したがって、注文は が完全に不要です。それはなぜ順序付けられていないのですか? [これを読む](http://stackoverflow.com/questions/6384710/why-is-a-dictionary-not-ordered) –

+0

私はいくつかの異なるフィールド値を持つそれらのエントリだけを数えたい場合(例:LinkedId )?私はこのようなものを試しましたが、expecteDとしては動作しません。新しい{adminid = g.Key、count = g.Select(x => x.LinkedId).Distinct()。Count()} –

関連する問題