FodderZone
FodderZone

Reputation: 883

C# SqlMethods Like Not Working

I'm brand new to using Linq and can't get the SqlMethods.Like function work.

Here's 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 filtered comes back empty. What am I missing?

If I use x.Equals("100") I get results back.

Upvotes: 4

Views: 9204

Answers (4)

MethodMan
MethodMan

Reputation: 18863

Could you use Contains for example

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.

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can use Contains operator instead Like

Upvotes: 0

Saurabh R S
Saurabh R S

Reputation: 3177

In your case use:

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

Upvotes: 1

Seth Flowers
Seth Flowers

Reputation: 9190

SqlMethods.Like

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

Your query is not a LINQ to SQL query.

Upvotes: 8

Related Questions