Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ Group By Key Format Date?

Posted on 2013-11-10
4
Medium Priority
?
1,932 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

886 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