Solved

LINQ Group By Key Format Date?

Posted on 2013-11-10
4
1,810 Views
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. "

[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();
0
Comment
Question by:WorknHardr
  • 2
  • 2
4 Comments
 
LVL 62

Accepted Solution

by:
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) 
             }).ToList();
             
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

0
 

Author Comment

by:WorknHardr
ID: 39640029
Wow, great code!

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

           EntityFunctions.TruncateDate(j.Budget.Created)
0
 
LVL 62

Expert Comment

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

Glad that worked out for you.
0
 

Author Closing Comment

by:WorknHardr
ID: 39640747
Thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now