C # SqlMethods how not working

I am new to using Linq and cannot get the SqlMethods.Like function.

Here is my simplified example:

List<string> mylist = new List<string>(); mylist.Add("100"); mylist.Add("101"); mylist.Add("102"); mylist.Add("103"); mylist.Add("104"); mylist.Add("105"); mylist.Add("106"); mylist.Add("107"); var filtered = mylist.Where(x => SqlMethods.Like(x, "10%")); foreach (var record in filtered) { textBox1.Text += record + "\n"; } 

My variable called filter is returned empty. What am I missing?

If I use x.Equals ("100"), I return the results.

+4
source share
4 answers

SqlMethods.Like

Determines whether a particular character string matches the specified pattern. This method is supported only in LINQ to SQL queries.

Your query is not a LINQ to SQL query.

+8
source

In your case, use:

 var filtered = mylist.Where(x => x.Contains("10")); 
+1
source

You can use Contains operator instead of Like

0
source

Could you use Contains

 var filtered = mylist.Where(x => x.Contains("10")); 

If you really want SQL LIKE , you can use System.Data.Linq.SqlClient.SqlMethods.Like(...) , which LINQ-to-SQL maps to LIKE in SQL Server.

0
source

Source: https://habr.com/ru/post/1438716/


All Articles