2012-02-20 53 views
2

我試圖查詢一個列表,但無法得到結果我想要的方式,林有點新來linq所以我確信這對你們來說很容易。Lambda表達式的幫助

對象:

public class myObject 
{ 
    public string Date { get; set; } 
    public string Log { get; set; } 
} 

我想查詢列表來自:

public List<myObject> getMyObjects() 
{ 
    // Code to get objects, ill leave it out here and return new list for this example 
    return new List<myObject>(); 
} 

結果我希望得到的是不同日期的列表和日誌的數量在每個日期,虐待提供我下面的不良嘗試

var result = (from data in errorList 
        select new 
        { 
         datum = data.Datum.Distinct(), 
         antal = data.Felkod 
        }).GroupBy(x => x.datum); 

我如何以正確的方式做到這一點?幫助會非常讚賞

回答

5
var query = errorList.GroupBy(x => x.Date) 
        .Select(g => new { Date = g.Key, Count = g.Count() }); 

var query = from error in errorList 
      group error by error.Date into g 
      select new { 
       Date = g.Key, 
       Count = g.Count() 
      }; 
+0

它只是令人驚訝的是多麼容易看起來,當有人得到它的權利,感謝名單了很多! – Andreas 2012-02-20 14:31:15

+0

如果你只能編輯第一個公式,那麼在最後一個公式前缺少結尾) – Andreas 2012-02-20 14:37:58

+0

完成,謝謝。抱歉。 – jason 2012-02-20 16:02:44