2011-02-03 66 views
49

我找對象LINQ的不同 - 計數

Date  Username 

01/01/2011 james 
01/01/2011 jamie 
01/01/2011 alex 
01/01/2011 james 
02/01/2011 matt 
02/01/2011 jamie 
02/01/2011 alex 
02/01/2011 james 
02/01/2011 james 
02/01/2011 lucy 
02/01/2011 alex 
03/01/2011 james 
03/01/2011 bob 
03/01/2011 bob 
03/01/2011 james 
03/01/2011 james 
04/01/2011 alex 
04/01/2011 alex 
04/01/2011 alex 

我想使用LINQ查詢日期列表具有獨特的用戶登錄次數的例子名單進行查詢。

例如:

01/01/2011 - 3 
02/01/2011 - 5 
03/01/2011 - 2 
04/01/2011 - 1 

我曾嘗試爲測試了一些LINQ語句,但都未給我想要的結果。我得到的最接近的是給我不同的日期,但有一個所有的用戶數。

任何幫助將不勝感激。

+9

請加你想這些LINQ語句 - 然後我們就可以明白爲什麼他們錯在哪裏。 – 2011-02-03 09:46:08

回答

92
logins 
    .GroupBy(l => l.Date) 
    .Select(g => new 
    { 
    Date = g.Key, 
    Count = g.Select(l => l.Login).Distinct().Count() 
    }); 
+4

我是Linq的新手,我想知道如何使用Linq查詢語法來編寫此代替Linq方法的語法。 – comecme 2011-02-03 11:26:28

+1

@comecme我意識到你在很久以前就離開了這個評論,但是看看我的翻譯到下面的方法語法。 – Kevin 2014-12-18 21:33:49

+1

我怕你的代碼總是返回Count = 1,這是因爲你添加了Disctinct()並且將使得計數一個元素。如果你刪除它,你將擁有數量的元素。希望這可以幫助!!! ;) – 2015-11-24 14:40:08

5

也許這樣的事情?

var list = new List<MyClass>(new[] { 
     new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "alex" }, 
     new MyClass { Date = DateTime.Parse("01/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "matt" }, 
     new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "jamie" }, 
     new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "alex" }, 
     new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "lucy" }, 
     new MyClass { Date = DateTime.Parse("02/01/2011"), Username = "alex" }, 
     new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "bob" }, 
     new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "bob" }, 
     new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("03/01/2011"), Username = "james" }, 
     new MyClass { Date = DateTime.Parse("04/01/2011"), Username = "alex" }, 
     new MyClass { Date = DateTime.Parse("04/01/2011"), Username = "alex" }, 
     new MyClass { Date = DateTime.Parse("04/01/2011"), Username = "alex" } 
    }); 

list.GroupBy(l => l.Date, l => l.Username) 
    .Select(g => new { 
       Date = g.Key, 
       Count = g.Distinct().Count() 
      }); 
1

另一種方式來解決這個問題是要組兩次,檢查樣品

  var dist = listLogins.GroupBy(d => d.date + d.Username) 
       .Select(x => x.First()) 
       .GroupBy(d => d.date).Select(y => new { date = y.Key, count = y.Count() }).ToList(); 
6

可以單一的GroupBy調用內完成,

var Query = list.GroupBy(
       (item => item.DateTime), 
       (key, elements) => new { 
              key = key, 
              count = elements 
                .Distinct() 
                .Count() 
             } 
       ); 
14

我意識到這是一個古老的問題,但我碰到它,看到了關於想要方法語法的評論,並不由自己去回答它...我可能有編碼障礙。

在查詢語法,它看起來像這樣......注意,是Distinct並沒有查詢語法Count

from l in logins 
group l by l.Date into g 
select new 
{ 
    Date = g.Key, 
    Count = (from l in g select l.Login).Distinct().Count() 
}; 

對於並排比較原始的方法語法側(我喜歡那些個人更好)在這裏你去...

logins 
    .GroupBy(l => l.Date) 
    .Select(g => new 
    { 
    Date = g.Key, 
    Count = g.Select(l => l.Login).Distinct().Count() 
    });