Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ Group By Key Format Date?

Posted on 2013-11-10
4
Medium Priority
?
1,911 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
[X]
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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) 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.…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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