In SQL we have LIKE operator to fetch the records which contains given string like below.
SELECT * FROM Employee WHERE name LIKE '%roy%'
Above query will return the employee records whose name contains "roy". W can achieve the same thing in LINQ by using Contains operator as shown below.
string[] employees = { "roy", "John", "Dennies" , "Abhi roy"};
IEnumerable<string> val = employees.Where(e=>e.Contains("roy"));
foreach (string name in val)
Console.WriteLine(name);
Console.ReadLine();
The above code returns employee names as "roy" and "Abhi roy".
If we want to get names which starts with the given string, the SQL query is like below.
SELECT * FROM Employee WHERE name LIKE 'roy%'
To achieve the above thing in LINQ, use StartsWith operator as shown below.
string[] employees = { "roy", "John", "Dennies" , "Abhi roy"};
IEnumerable<string> val = employees.Where(e=>e.StartsWith("roy"));
foreach (string name in val)
Console.WriteLine(name);
Console.ReadLine();
The above code returns employee name "roy".
If you want to get records whose names end with "roy" use below SQL query.
SELECT * FROM Employee WHERE name LIKE '%roy'
To achieve the above thing in LINQ, use EndsWith operator as shown below.
string[] employees = { "roy", "John", "Dennies" , "Abhi roy"};
IEnumerable<string> val = employees.Where(e=>e.EndsWith("roy"));
foreach (string name in val)
Console.WriteLine(name);
Console.ReadLine();
The above code returns employee name "Abhi roy".