2

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

1 Answer 1

5
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
Sign up to request clarification or add additional context in comments.

2 Comments

Its just amazing how easy it looks when someone gets it right, thanx alot !
If you can just edit the first formula, its missing the ending } before the last )

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.