Dave Mackey
Dave Mackey

Reputation: 4432

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

Upvotes: 2

Views: 8491

Answers (2)

André Kops
André Kops

Reputation: 2713

I got something similar working like this - in C# but the solution should work in VB too:

string.Concat(string.Concat(p.FIRST_NAME, " ", p.MIDDLE_NAME, " "), p.LAST_NAME)

It doesn't support over 4 items so you have to nest them.

Upvotes: 1

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234484

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.

Upvotes: 4

Related Questions