Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

LINQ Group By Key Format Date?

The group by Year/Month works fine, just cannot format the Key to a datetime. Keep getting this error:

" LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression. "

[Linq]
var model = (from b in context.Budgets
                         let j = new ViewModel { Budget = b }
                         let dt = j.Budget.Created
                         group j by new { y = dt.Year, m = dt.Month } into g
                         select new Group<string, ViewModel>
                         {
                             Key = Convert.ToDateTime(g.Key).ToString("MM/yyyy"),
                         }
                          ).ToList();
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WorknHardr
WorknHardr

ASKER

Wow, great code!

I'm surprise Entity doesn't have a function like so:

           EntityFunctions.TruncateDate(j.Budget.Created)
Maybe in some future release.

Glad that worked out for you.
Thx