Solved

Linq Grouping By Day

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

12 Experts available now in Live!

Get 1:1 Help Now