score:5
.groupby(category => category.itemcategory);
returns an enumerable of igrouping objects, where the key of each igrouping is a distinct itemcategory value, and the value is a list of moneyspent objects. so, you won't be able to simply drop these groupings into an observablecollection as you're currently doing.
instead, you probably want to select each grouped result into a new moneyspent object:
var finalquery = boughtitemstoday
.groupby(category => category.itemcategory)
.select(grouping => new moneyspent { itemcategory = grouping.key, itemamount = grouping.sum(moneyspent => moneyspent.itemamount);
boughtitems = new observablecollection<dbcontrol.moneyspent>(finalquery);
score:0
you can project each group to an anyonymous (or better yet create a new type for this) class with the properties you want:
var finalquery = boughtitemstoday.groupby(category => category.itemcategory);
.select(g => new
{
itemcategory = g.key,
cost = g.sum(x => x.itemamount)
});
the asqueryable()
should not be needed at all since boughtitemstoday
is an iquerable
anyway. you can also just combine the queries:
var finalquery = boughtitemdb.boughtitems
.groupby(item => item.itemcategory);
.select(g => new
{
itemcategory = g.key,
cost = g.sum(x => x.itemamount)
});
Source: stackoverflow.com
Related Query
- Using LINQ on ObservableCollection with GroupBy and Sum aggregate
- Using Linq to GroupBy and Sum datatable
- LINQ Sum Nested collection with GroupBy and OrderByDescending
- Linq to SQL using Lambda expressions together with Join, GroupBy, Count and Sum
- Linq Query with using sum and order by
- How to sum values for multiple columns in DataTable using Groupby with Linq
- GroupBy and Count with condition using LINQ
- LINQ with sub select query using sum and group by?
- GROUPBY and SUM on List items using LINQ
- How to group multiple lists using LINQ - Sum and GroupBy
- Linq Join with GroupBy and Sum
- how to execute a nested group aggregate functions with multi groupBy columns using entity framework and linq?
- Can LINQ GROUPBY and SUM with Dapper achieve this?
- LINQ with groupby and aggregate always shows default value
- How sum a column in Entity Framework using linq and join it with another table
- convert SQL query with multiple join on multiple tables using group by on multiple columns and with aggregate function into LINQ
- vb linq select from two datatables using left join with grouping and giving Sum
- Convert Datatable GroupBy Multiple Columns with Sum using Linq
- LINQ with groupby and count
- Using GroupBy, Count and Sum in LINQ Lambda Expressions
- Using LINQ to group by multiple properties and sum
- Translate SQL to lambda LINQ with GroupBy and Average
- Using Distinct with LINQ and Objects
- Linq Query with SUM and ORDER BY
- How to update an element with a List using LINQ and C#
- LINQ Query with GROUP and SUM
- Difference between Sum and Aggregate in LINQ
- Simple sql to Linq query with group by and aggregate functions
- Linq GroupBy and Aggregate
- Convert a list to a dictionary and sum up values using linq
More Query from same tag
- Flatten LINQ subselect so 1..n becomes one row
- How do I get a spatial value from EF Core's FromSqlRaw
- linq query where int ID belongs to List<int>
- Can I refer to items within a LINQ result set by index?
- LINQ performance improvements
- Access LINQ-2-SQL DataContext in entity class
- How to get the List of products name whose name starts with numbers using LINQ query.?
- Mapping of nested IEnumerable<IDictionary> to a different type of nested IEnumerable<IDictionary>
- How to format a string in a linq expression?
- Dynamic lambda expression for SingleOrDefault
- Remove records from one datatable based on records from another datable
- IEnumerable - CopyToDataTable() Not Display
- Retrieve ID from database in mvc
- Based on date, get todays and the next two days values from XML?
- Get index of parameter of element in list
- .net + SQL + Timeout issue
- How to create complex Custom object from LINq
- List.Cast<> error "is a method which is not valid in the given context"
- Getting linq error with group
- Select and sum DataTable rows with criteria
- Use linq to break up list<t> into lots of list<t> of n length?
- LINQ to SQL with JOIN, GROUP BY, HAVING and MAX
- Unable to fetch data from Azure Cosmos DB
- Sending an Entity on email using Mailer ASP.net
- Order by alphabet letters A-L and M-Z using lambda
- Coverity, Enumerable.Where(this ...), and IDisposable
- directory.getfiles stops searching when error
- Loop through IEnumerable<object>
- c# RegEx what line is the match on. Linq?
- Trying to parse XML and create a List information using Linq and C#