0

I'm trying the following query:

Return _context.companies _
        .Where(Function(c) c.id.StartsWith(filterCompanyId)) _
    .ToList()

but because id is an integer, it doesn't work. I found this answer which suggests I can use

c.id.ToString().StartsWith ...

but that gives me the following error:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

When trying to use

.Where(Function(c) SqlFunctions.StringConvert(CDbl(c.id)).StartsWith("1"))

it doesn't work.

Any tips?

Community
  • 1
  • 1
Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104

2 Answers2

1

The relevant part is hidden in the error message:

'SqlFunctions' is not declared. It may be inaccessible due to its protection level

SqlFunctions is in the System.Data.Objects.SqlClient namespace (you can discover this by going to the "SqlFunctions Class" MSDN page and then checking the "Namespace:" entry). Thus, you need to import that namespace at the top of your code file:

Imports System.Data.Objects.SqlClient
Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

Per @Heinzi's comment, StringConvert is not supported by the MySQL-EF-Provider. I ended up going with a query string:

Dim isExpiredInt As Integer = If(isExpired, 1, 0)

Dim query As String = "Select * FROM company WHERE (expired = 0 OR expired = {0}) AND name like {1} AND id like {2}"

Dim companies = _context.ExecuteStoreQuery(Of company)(
    query,
    "companies",
    System.Data.Objects.MergeOption.AppendOnly,
    isExpiredInt, "%" & filterCompanyName & "%", filterCompanyId & "%"
).ToList()

Return companies

Note the System.Data.Objects.MergeOption.AppendOnly is something I added so the objects from this query would be attached to my context (i.e. calling SaveChanges on this context would write to the database).

Hope that helps!

Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104