LINQ StartsWith

By Riley K
Access over 40 UI widgets with everything from interactive menus to rich charts.

Equivalent of Like opertor in LINQ


Normally in SQL we use LIKE Operator to get results starting with certain character

In LINQ there is no LIKE method instead its Equivalent in to use StartsWith Method

For example I got a list of these strings

List<string> lstNames = new List<string>();
            lstNames.Add("Riley");            
            lstNames.Add("McCarty");
            lstNames.Add("Mike");
            lstNames.Add("Micheal");
            lstNames.Add("Morisson");
            lstNames.Add("Micky");
            lstNames.Add("John");
            lstNames.Add("Riley Keller");

Now i want to display the results starting with 'M', Here is how you query it

var result = lstNames.Where(name => name.StartsWith("M"));

            foreach (var s in result)
             {
                 Console.WriteLine(s.ToString());
            }
Here is the output





LINQ StartsWith  (1279 Views)