Muzammil Anwar
Muzammil Anwar

Reputation: 15

SQL wildcard _ in Linq

I have a query

select Max(CompanyId) from EmployeeCompany where CompanyId like '01-001' +'-___'

How can I implement '___' in Linq any idea? I have companiyId like 01-001-111, 01-001-112

Upvotes: 0

Views: 159

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

With Entity Framework you can use SqlFunctions.PatIndex

  db.EmployeeCompany
    .Where(c => SqlFunctions.PatIndex("01-001-___", c.CompanyId) > 0)
    .Max(c => CompanyId)

NOTE: This function will not generate SQL query with LIKE operator. Instead it will generate query with PATINDEX function (which seems to be faster than LIKE operator).

Upvotes: 2

Related Questions