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

x
?
Solved

Linq Grouping By Day

Posted on 2014-04-09
3
Medium Priority
?
264 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
[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
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

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