3

ActivityLogのレコードを含む次の表があります。

ID, UserId,      Category,   Created
1       11     DeAssigned    05/10/2012
2       11          LogIn    05/11/2012
3       20       Assigned    06/15/2012
4       11       Assigned    06/10/2012
5       20     DeAssigned    06/13/2012
6       20       Assigned    07/12/2012
7       11     DeAssigned    07/16/2012
8       20       Assigned    08/15/2012
...

ここで、テーブルにクエリを実行して、次のような同じ構造体を作成します

var data = new[] { 
                    new { Month = "05", Assigned = 14, DeAssigned = 5, LogIn=1 },
                    new { Month = "06", Assigned = 5, DeAssigned = 2, LogIn=0 },
                    new { Month = "07", Assigned = 50, DeAssigned = 8, LogIn=0 },
                    new { Month = "08", Assigned = 15, DeAssigned = 1, LogIn=0 }
};

私が達成したこと:

var result = (from l in repoA.GetAll()
                      where   l.Created >= System.DateTime.Now.AddYears(-1)
                      group l by new { l.Created.Value.Month, l.Category }
                      into groups
                      orderby  groups.Key.Month 
                      select new
                                 {
                                     Month = groups.Key.Month,
                                     Category = groups.Key.Category,
                                     Count = groups.Count()
                                 });

最適な結果はありませんが、月ごとにグループ化されたすべてのアクティビティをカウントします。

 [0] {Month = 6, Category = Assigned, Count = 2}
 [0] {Month = 6, Category = Designed, Count = 1}
 [0] {Month = 6, Category = LogIn, Count = 1}
 [0] {Month = 7, Category = Assigned, Count = 3}

テーブルをクエリして、結果を「水平カウント」形式にフォーマットするにはどうすればよいですか?

4

1 に答える 1

2

またはもっと単純に:

var result = (from l in repoA.GetAll()
                           where   l.Created >= System.DateTime.Now.AddYears(-1) 
                           group l by l.Created.Month into groups
                           orderby groups.Key ascending
                           select new
                              {
                                  Month = groups.Key,
                                  Assigned = groups.Where(g => g.Category == "Assigned").Count(),
                                  Deassigned = groups.Where(g => g.Category == "DeAssigned").Count(),
                                  LogIn = groups.Where(g => g.Category == "LogIn").Count()
                              });
于 2012-06-29T10:19:08.127 に答える