LINQ Group By Key Format Date?

Posted on 2013-11-10
Last Modified: 2016-02-10
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. "

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"),
Question by:WorknHardr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 63

Accepted Solution

Fernando Soto earned 500 total points
ID: 39637793
Hi WorknHardr;

In this statement, "Convert.ToDateTime(g.Key).ToString("MM/yyyy")", g.Key is an anonymous data type and can not be converted to a DateTime object with the Convert.ToDateTime method because there is no method that accepts an anonymous type as a parameter. But if it did Linq to Entity Framework doest not implement the ToString() method into a SQL native statement. What you can do in this case is the following

using System.Data.Objects.SqlClient
// You will also need to add a reference to System.Data.Entity.dll

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
             let month = (double) g.Key.Month
             let year = (double) g.Key.Year
             select new Group<string, ViewModel>
             	   Key = SqlFunctions.StringConvert(month, 2) + "/" + SqlFunctions.StringConvert(year, 4) 
foreach (var element in results)
	 // Because the above query will not fill in 0 in positions where there is no digit you will need to replace String " " with "0".
         // Not knowing what the structure of the object Group<string, ViewModel> is I will leave this to you to do using a String.Replace(" ", "0")

Open in new window


Author Comment

ID: 39640029
Wow, great code!

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

LVL 63

Expert Comment

by:Fernando Soto
ID: 39640607
Maybe in some future release.

Glad that worked out for you.

Author Closing Comment

ID: 39640747

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question