none
Visual Basic : LINQ to Entities does not recognize the method 'System.String ToString()' method RRS feed

  • Question

  • This is a VB.NET application where I would like to provide the user w/the capability to search for a project by Year.

    The user would input something like "2014" (as String).

    Neither of the following 2 options is working:

    Dim projects = From p In db.Projects Where p.DateCreated.ToString.Contains(projectYear) Select p
    
    
    Dim query = From p In db.Projects Where p.DateCreated.ToString.Contains(projectYear)
    Dim projects = From c In query.AsEnumerable Select c

    The Table definition for the "DateCreated" field is:

    [DateCreated]   DATE           NULL,

    I have not found a solution for VB (only for C#) so far.

    Thursday, May 22, 2014 4:40 PM

Answers

  • One possible solution turned out to be :

    Dim listOfProjects As New List(Of Project)
    Dim projects = From p In db.Projects Select p
    For Each project In projects
      If project.DateCreated.ToString.Contains(projectYear) Then
                        listOfProjects.Add(project)
      End If
    Next
    
    ...
    ...
    ...
    
    Return View("~/Views/Project/SearchResults.vbhtml", listOfProjects)

    Not sure of its elegance, but it serves its purpose.

    • Edited by Fan Farron Friday, May 23, 2014 11:15 PM
    • Marked as answer by Fan Farron Friday, May 23, 2014 11:15 PM
    Friday, May 23, 2014 10:55 PM

All replies

  • Hi Farron,

    The problem arises because ToString method isn’t really executed, it is turned into a MethodGroup and then parsed and translated to SQL. Sine there is no ToString equivalent, the expression fails.

    What you should do is to choose other method to achieve your goal.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 23, 2014 6:22 AM
    Moderator
  • One possible solution turned out to be :

    Dim listOfProjects As New List(Of Project)
    Dim projects = From p In db.Projects Select p
    For Each project In projects
      If project.DateCreated.ToString.Contains(projectYear) Then
                        listOfProjects.Add(project)
      End If
    Next
    
    ...
    ...
    ...
    
    Return View("~/Views/Project/SearchResults.vbhtml", listOfProjects)

    Not sure of its elegance, but it serves its purpose.

    • Edited by Fan Farron Friday, May 23, 2014 11:15 PM
    • Marked as answer by Fan Farron Friday, May 23, 2014 11:15 PM
    Friday, May 23, 2014 10:55 PM