score:2
since you also want to order the results by dayofweek
, using a dictionary
is not appropriate, and you should start with a view model to represent what you want in the view
public class salesvm
{
public dayofweek day { get; set; }
public int sales { get; set; }
}
in order to prevent the exception, materialize your query first (using tolist()
or .asenumerable()
) and i suggest you just satrt by selecting only the data you need from the database
viewbag.dailygraph = ctx.storeitemtransactions
.select(x => new { date = x.date, sales = x.sales }) // select only required data
.tolist() // materialize
.groupby(x => x.date.dayofweek) // group by day of week
.select(x => new testvm() { day = x.key, sales = x.sum(y => y.sales) }) // project to view model
.orderby(x => (int)x.day); // order by day of week
score:2
linq to entities does not know how to tranlsate that property of datetime to sql, you need to use sqlfunctions.datepart method for getting the dayofweek in the groupby which can be done following way :
var result = ctx.storeitemtransactions
.groupby(x => sqlfunctions.datepart("weekday", x.transactiondate))
.select(pr => new hourlygraph
{
day = pr.key.tostring(),
sales = pr.sum(x => x.quantitysoldtransaction)
});
Source: stackoverflow.com
Related Query
- LINQ group by day of the week throwing exception
- Linq - Group by week on the List
- How to group by day of week using linq
- c# linq with group by and join throwing exception
- Linq lambda expression - Find the closest day of week to current date (now)
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- How do I get the MAX row with a GROUP BY in LINQ query?
- Convert string to int in an Entity Framework linq query and handling the parsing exception
- LINQ throwing invalid cast exception on a bigint
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Access all of the data after joining two tables and group them using linq
- Linq to Entities group by week number
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ to Entities does not recognize the method exception
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Linq OrderBy to group objects with the same sets
- Linq - group by using the elements inside an array property
- Left outer join using LINQ -- understanding the code
- Using GroupBy in linq to group the letters of a string
- How does linq actually execute the code to retrieve data from the data source?
- Why isn't this LINQ Group By aggregating the rows in vb.net?
- LINQ Source Code Available
- How do I order the elements in a group by linq query, and pick the first?
- How can I group by the difference between rows in a column with linq and c#?
- LINQ - Writing an extension method to get the row with maximum value for each group
- Linq to SharePoint throwing null reference exception
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- LINQ MySQL Group by Year, Month, Day select Year, Month, Day, Count
- C# Linq or Lambda expression Group by Week IEnumerable list with date field
More Query from same tag
- Expression.Call - Calling linq extension: FirstOrDefault, Where
- How to set two values in View()
- Order list by parent and child items
- How to structure classes to collect data, analyse data, summarize analysis for quick reference
- Help to convert to linq
- Using LINQ to Calculate Depreciation
- What is the difference between ((IEnumerable)source).OfType<T>() and source as IEnumerable<T>
- Can't add/sum string values in IEnumerable
- Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Data.DataRow]' to same type
- Generating XML by using XSD class
- selecting a property from an object using Sequences in F#
- Displaying records for two weeks only! from creation
- Sorting a 1-to-many relationship for a log table for the most recent item
- average of first 3 items from list using linq
- Dynamic Linq to OrderBy Object Nested in IEnumerable
- Query from relational tables with conditions using Entity Framework c#
- LinQ to XML query
- LINQ sorting: Cannot order by type 'System.IComparable'
- Method GetPrice() cannot be translated into a store expression
- Entity Framework - Insert multiple complex objects at once
- Convert IEnumerable object list to list of object arrays C#
- conditional where in linq query
- C# Code Optimization GroupBy
- In what scenarios is LINQ best applicable?
- Split collection linq entity framework
- Checking presense of a point in multiple rectangles usinq LINQ
- How do I print different names from a list?
- Why can't I query this range?
- System.NullReferenceException in a linq sort OrderBy(a => a.Url), UWP
- C# For Loop with LIST using LINQ