Solved

Linq Grouping By Day

Posted on 2014-04-09
3
252 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 62

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 62

Accepted Solution

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

13 Experts available now in Live!

Get 1:1 Help Now