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 Query
- 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 - how to group by a 'let' value and sum another 'let' value
- How to group by and sum for every 7 days using linq lambda?
- How should i use group by and sum on different values of list of models using linq
- Use LINQ to group by one field, sum based on group AND store on Dictionary
- How can I simultaneously group and sum a list of items using Linq?
- How can I group and sum XML data with the following structure using LINQ?
- How to group by and sum in linq when expressions in sum are string?
- how can i get the right fields into a group and sum using linq?
- How to group table data and sum by multiple columns using LINQ or another approach
- Linq to SQL With Left Outer Join and Group By With Sum - How To
- How to Group and Sum List of Lists using Linq
- How to group multiple lists using LINQ - Sum and GroupBy
- How to extract values from a list of dictionaries and group and convert to a single dictionary with Linq?
- How to make 2 dictionary and group it twice
- How to group by and calculate sum and select where is less than given amount in SQL Server / C# Linq
- How to convert sql to LINQ combine LEFT JOIN, WHERE, GROUP BY and SUM in LINQ?
- How to group by and sum operations Datatable
- How to group and sum up in a LINQ like manner in JavaScript
- How to use LINQ to group by one field, sort within that grouping, and sum values for that combination?
- How to select columns and sum of columns using group by keyword from data table in c#
- How to Group and sum values of two Column in C# Linq
- How to Group by a field and Sum another field in codefirst?
- How to convert this sql query into linq query. I am stuck at the group by and sum part
- How to use SUM and GROUP BY function in LINQ
- How to use group by and also sum on a database record using linq
- How to combine LEFT JOIN, GROUP BY and SUM in LINQ?
- How to return sum value from using group by, having and sum by using LINQ in C#
- How to group datatable by unknown column names and calculate sum of one field?
More Query from same tag
- Null Object Reference on Retrieving Nested XML Elements with Linq / C#
- How to nested parsing expression .NET C#
- How do I create hierarchical query with where condition on the children collection
- LINQ: Counting Total Number of Nested Elements
- Apply distinct on the basis of item2 of List of integer tuples in c#
- How to Query to build a menu using LinQ in C#
- Need help optimizing loop with Linq
- using LINQ to search in 2 different elements in XML file
- How can I query different tables dynamically with LINQ?
- How can I use this extension method in a linq to entities query using query syntax?
- Getting MemberExpression, expecting ConstantExpression
- Linq - Retrieve value from a linked linked table
- Xml linq query based on 2 Elements
- Odata and Select Issues
- Compare dates linq c#
- LINQ to SQL sum null value
- Why Linq Query Return Duplicate column record
- how to add hours and minutes using canonical functions in linq
- Linq - One to many relationship, order by rules
- How to convert groupby query to linq and compute other thing while executing?
- How to create dynamic LINQ Expression with multiple binary expression conditions and a contains condition
- Ordering lists within a list in C#
- LINQ with dynamic where clause in EFCore
- Linq: forward looking condition
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- c# linq to xml XElement group by ancestor attribute to create list of entities collection with key
- Merge Lists within List that share same property C#
- LINQ Query Issue
- Update between 2 datatables using linq/lambda
- EF4 Linq - Get top items using criteria from related tables