Ask Your Question
4

How can we group data using Linq based on dates and then get the count of each group?

asked 2022-04-21 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-09-26 12:00:00 +0000

nofretete gravatar image

Assuming we have a list of objects with a date property called "Date":

List<MyObject> myObjects = new List<MyObject>();

We can use Linq to group the objects by date and get the count of each group like this:

var groupedData = myObjects
    .GroupBy(o => o.Date.Date)
    .Select(g => new { Date = g.Key, Count = g.Count() });

This groups the objects by the date portion of the "Date" property and returns an anonymous type with two properties: "Date" (the date being grouped by) and "Count" (the number of objects in that group).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-21 11:00:00 +0000

Seen: 1 times

Last updated: Sep 26 '22