Like operator in Linq to DataTable?

haansi picture haansi · Dec 16, 2010 · Viewed 8.5k times · Source

I am using Linq to DataTable. How I can apply like operator in where clause. I want to do a search on data just as we have like operator in SQL.

I searched and tried the following code but got an error: Method 'Boolean Like(System.String, System.String)' cannot be used on the client; it is only for translation to SQL.

var details = from addresses in dt.AsEnumerable() 
    where SqlMethods.Like(prefixText, prefixText + "%") || SqlMethods.Like(prefixText, "%" + prefixText + "%")
    select (string) addresses["Details"];                      

return details.ToArray();

Answer

Marc Gravell picture Marc Gravell · Dec 16, 2010

Your best bet may be to re-write it as a regex an use

where yourRegex.IsMatch(row.SomeValue)

Or if it is just starts-with queries:

where row.SomeValue.StartsWith(prefix)