Im trying to query a List but unable to get the result the way i would like, im kinda new to linq so im sure this will be quite easy for you guys.
Object:
public class myObject
{
public string Date { get; set; }
public string Log { get; set; }
}
The List i would like to query comes from:
public List<myObject> getMyObjects()
{
// Code to get objects, ill leave it out here and return new list for this example
return new List<myObject>();
}
The result i would like to get is a list with distinct dates and the number of logs on each date, ill provide my bad attempt below
var result = (from data in errorList
select new
{
datum = data.Datum.Distinct(),
antal = data.Felkod
}).GroupBy(x => x.datum);
How would i do this the correct way? Help would be much appreciated
Graham Borland
60.8k21 gold badges144 silver badges184 bronze badges
asked Feb 20, 2012 at 14:02
Andreas
3011 gold badge4 silver badges16 bronze badges
1 Answer 1
var query = errorList.GroupBy(x => x.Date)
.Select(g => new { Date = g.Key, Count = g.Count() });
or
var query = from error in errorList
group error by error.Date into g
select new {
Date = g.Key,
Count = g.Count()
};
answered Feb 20, 2012 at 14:04
jason
243k36 gold badges437 silver badges532 bronze badges
lang-cs