score:1
g.GroupBy(gr => new DateTime(gr.Time.Year, gr.Time.Month, 1))
.ToDictionary(i => i.Key, i => i.Sum(s => s.TradeRequirement));
score:1
You can get both: Sum
and Average
at the same time, using anonymous type:
var tradeReqsBySegment = segGroups.Join(pPeriods, s => s.MarketSegmentId, p => p.EntityId, (s, p) => new
{
SegmentCode = s.SegmentCode, // string
Time = p.StartLocal, // datetime
TradeRequirement = p.Volume // double
})
.GroupBy(s => s.SegmentCode)
.ToDictionary(g => g.Key,
g => g.GroupBy(gr => new DateTime(gr.Time.Year, gr.Time.Month, 1))
.ToDictionary(gr => gr.Key.ToString("MM/yyyy"),
gr => new {
Sum = gr.Sum(s => s.TradeRequirement),
Avg = gr.Average(s => s.TradeRequirement)
}));
Source: stackoverflow.com
Related Articles
- How to group dictionary by dateTime and do a sum on int
- How to group a Dictionary by DateTime and average results using Linq
- LINQ Group By into a Dictionary Object
- LINQ: Group by month and year within a datetime field
- Group into a dictionary of elements
- Group a collection and return a Dictionary
- Group DateTime by an arbitrary time interval
- LINQ Source Code Available
- how do i use group by only on date from datetime in LINQ query
- Find max and min DateTime in Linq group
- .NET 4 Code Contracts: "requires unproven: source != null"
- Group DateTime rows in datatable by DateTime - C#
- Group By and To Dictionary in EF Core 3.1
- linq group by a start time to endtime on DateTime Column
- LINQ group by multiple attributes and create dictionary
- How to sort dictionary by DateTime value
- Linq group by DateTime / intervals
- List to Dictionary with Group in Linq
- C# Linq group by DateTime ignore hours and minutes
- Code Example for Add DateTime and TimeSpan in EF query
- creating Linq to sqlite dbml from DbLinq source code
- Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate
- Better code for avoiding one dictionary - Case Sensitivity Issue
- Use LINQ to group by one field, sum based on group AND store on Dictionary
- Group objects by datetime interval
- group and count duplicates to a dictionary with linq
- Group Dictionary by splitting the Key and create new Dictionary
- How to group a list so that a single record for a day on a DateTime field is retrived using Linq?
- c# linq group by a datetime to the nearest second
- How to Select top (5) contributors group by Business type code in c# linq query
- Join and Include in Entity Framework
- LINQ group by and select random element from each group
- How to return a list of anonymous object in Linq to Sql on joining two tables
- Multiple linq "from" and variables visibility
- Select multiple table with Linq to Sql
- Linq query to select data from table and join 2 tables
- Linq deferred execution with local values
- EF: Is Foreign table of a foreign table included when using include?
- Converting IEnumerable<IEnumerable<T>> to List<string>
- How to get elements value with Linq To XML
- Entity class serialize to json
- Flatten a Dictionary<int, List<object>>
- Self JOIN linq with not equal
- Basic LINQ to Object codes
- Use ToDictionary to create a dictionary
- MVC gives a strange error
- How to "Linq to DataTable" using Own DataTable Class
- Why is linq slow in c#
- Linq vb.net: Find Item in DataRow[]
- Access to value in ASP.NET Model, not in database, for linq query