Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq equivalent to SQL LIKE [a-f]

SELECT * FROM Customer WHERE Name 'LIKE [a-f]%'

How Can I acheive this in Linq??

In other words in linq how can i select all Names between a and f??

Thanks,

like image 429
John Avatar asked Dec 17 '25 00:12

John


2 Answers

There's a helper class called SqlMethods in the System.Data.Linq.SqlClient namespace that provides a Like method that emulates the SQL LIKE statement.

Your query would be:

var query = from c in Customers
            where SqlMethods.Like(c.Name, "[a-f]%")
            select c;
like image 181
Mircea Grelus Avatar answered Dec 19 '25 13:12

Mircea Grelus


You could use C# Regex class to match records:

var selectedCustomers = from customer in customers
               where Regex.Match(customer.Name, "^[a-f].*$").Success
               select customer;
like image 23
Russell Avatar answered Dec 19 '25 13:12

Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!