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

x
?
Solved

Linq Grouping By Day

Posted on 2014-04-09
3
Medium Priority
?
268 Views
Last Modified: 2016-02-15
Can someone tell me where I'm going wrong here?

Dim q = From s In db.Spots Where s.StartDate >= New DateTime(2009, 2, 22) And s.StartDate <= New DateTime(2014, 5, 22) _
                Group s By day = s.StartDate.Value.Date Into g = Group _
                Select New With {.Date = day.ToString("dd/MM/yyyy"), .Count = g.Count()}

Open in new window


Locals shows 'Error' and if I do q.count, I get An unhandled exception of type 'System.NotSupportedException' occurred in System.Data.Linq.ni.dll
0
Comment
Question by:Dodsworth
  • 2
3 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39988659
Is this Linq to SQL or Linq to Entity Framework?
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39988679
sql
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39988758
Hi Dodsworth;

Linq to SQL does not translate the method ToString() into SQL type queries so therefore it can not be use. What you can do is in place of this statement in your query, .Date = day.ToString("dd/MM/yyyy"), replace it with this, .Date = day.Date, then before using it in your code convert it to a string in the correct format. Or you can create a class that will convert it for you automatically as shown below.

Dim q = From s In db.Spots _
        Where s.StartDate >= New DateTime(2009, 2, 22) And s.StartDate <= New DateTime(2014, 5, 22) _
        Group s By day = s.StartDate.Value.Date Into g = Group _
        Select New Data With 
        {
        	  .Date = day.Date, 
        	  .Count = g.Count()
        }
        
Public Class Data
    Private _date As String
    Public Property [Date]() As String
        Get
            Return _date
        End Get
        Set
            _date = CType(Value, DateTime).ToString("dd/MM/yyyy")
        End Set
    End Property
    
    Public Count As Integer
End Class

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

916 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