Search code examples
asp.netvb.netentity-frameworkentity-framework-4linq-to-entities

Concatenating String in LINQ-to-Entities


This code works if I take out

  • " " + p.MIDDLE_NAME

Thus the remaining code looks like:

p.FIRST_NAME + " " + p.LAST_NAME

Maybe LINQ-to-Entities just doesn't support concatenating more than two strings at a time?

    Protected Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click
    Dim dbContext As Campus6Entities = New Campus6Entities
    Using dbContext
        Dim find_students = From p In dbContext.PEOPLE _
                            Let Full_Name = p.FIRST_NAME + " " + p.MIDDLE_NAME + " " + p.LAST_NAME _
                            Where Full_Name = txtFilterText.Text _
                            Select p.FIRST_NAME, p.MIDDLE_NAME, p.LAST_NAME, p.PEOPLE_CODE_ID

        rptrFilteredStudents.DataSource = find_students
        rptrFilteredStudents.DataBind()
    End Using
End Sub

Solution

  • Ok, here's a guess. I just typed this into LinqPad:

    From x in Enumerable.Empty(Of String)().AsQueryable()
    Select x + " " + x
    

    And got this as the equivalent expression tree (sorry, LinqPad show this C#-y):

    System.String[]
       .Select (x => String.Concat (x, " ", x))
    

    Then I typed this:

    From x in Enumerable.Empty(Of String)().AsQueryable()
    Select x + " " + x
    

    And the expression tree became:

    System.String[]
       .Select (x => String.Concat (new String[] { x, " ", x, " ", x } ))
    

    I'm guessing this has something to do with the fact that Concat has overloads for up to four parameters, and then there is an overload that takes a parameter array. Maybe the LINQ provider does not support that last one.

    EDIT: According to this answer LINQ to entities does not current support string.Concat with something other than strings.