I have a Entity named "transactions" with {Id, RawDateTime, Score,SId}
I want to get a average of Score per day for records having SId. What as I doing wrong here?
_context.transactions.Where(b => b.SId == request.Id).GroupBy(a=>a.RawDateTime.Date).Include(b=>b.Average(v=>v.Score)).ToListAsync()
Tuhin PaulPosted Apr 5, 2023, 8:47 AM
Also as per your query the Include method is used to load related entities along with the main entity, but in your query, you are trying to include the result of the Average operation which is not a related entity. Instead of using Include, you can use Select to project the result of the query into the desired shape.
Another approach :
This query groups the transactions by date, calculates the average score for each group.
Tuhin PaulPosted Apr 5, 2023, 8:45 AM
Rajkiran SwainPosted Apr 5, 2023, 7:05 AM
Jignesh KumarPosted Apr 5, 2023, 6:54 AM
Please try below,
Amit MohantyPosted Apr 5, 2023, 4:13 AM
You're using the
Includemethod after theGroupBymethod, which is not allowed in Entity Framework. TheIncludemethod is used to specify related entities to be included in the query results, but it should be used before any grouping or aggregation operations.