Using the Contains method inside the Where method enables us to generate the SQL LIKE operator with the ‘%n%’ search team as below:
using EntityFrameworkCore_ConsoleApp.Models;
using System.Linq;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var products = (from product in databaseContext.Products
where product.Name.ToLower().Contains("vi".ToLower())
select product).ToList();
products.ForEach(product =>
{
Console.WriteLine("Id: " + product.Id);
Console.WriteLine("Name: " + product.Name);
Console.WriteLine("Price: " + product.Price);
Console.WriteLine("Quantity: " + product.Quantity);
Console.WriteLine("Description: " + product.Description);
Console.WriteLine("Status: " + product.Status);
Console.WriteLine("Photo: " + product.Photo);
Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
Console.WriteLine("Category Id: " + product.CategoryId);
Console.WriteLine("---------------------");
});
}
}
}
}
Output
Id: 9
Name: Tivi 1
Price: 10
Quantity: 15
Description: Description 1
Status: True
Photo: photo1.gif
Created: 20/10/2023
Category Id: 1
---------------------
Id: 10
Name: Tivi 2
Price: 5
Quantity: 22
Description: Description 2
Status: False
Photo: photo2.gif
Created: 20/04/2022
Category Id: 1
---------------------
Id: 11
Name: Tivi 3
Price: 20
Quantity: 4
Description: Description 2
Status: False
Photo: photo3.gif
Created: 20/11/2022
Category Id: 1
---------------------
If you want to check whether a sequence starts with a certain term, you can use the StartsWith method in C# as below:
using EntityFrameworkCore_ConsoleApp.Models;
using System.Linq;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var products = (from product in databaseContext.Products
where product.Name.ToLower().StartsWith("tivi".ToLower())
select product).ToList();
products.ForEach(product =>
{
Console.WriteLine("Id: " + product.Id);
Console.WriteLine("Name: " + product.Name);
Console.WriteLine("Price: " + product.Price);
Console.WriteLine("Quantity: " + product.Quantity);
Console.WriteLine("Description: " + product.Description);
Console.WriteLine("Status: " + product.Status);
Console.WriteLine("Photo: " + product.Photo);
Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
Console.WriteLine("Category Id: " + product.CategoryId);
Console.WriteLine("---------------------");
});
}
}
}
}
Output
Id: 9
Name: Tivi 1
Price: 10
Quantity: 15
Description: Description 1
Status: True
Photo: photo1.gif
Created: 20/10/2023
Category Id: 1
---------------------
Id: 10
Name: Tivi 2
Price: 5
Quantity: 22
Description: Description 2
Status: False
Photo: photo2.gif
Created: 20/04/2022
Category Id: 1
---------------------
Id: 11
Name: Tivi 3
Price: 20
Quantity: 4
Description: Description 2
Status: False
Photo: photo3.gif
Created: 20/11/2022
Category Id: 1
---------------------
If you want to check whether a sequence ends with a certain term, we can use the EndsWith method in C#.
using EntityFrameworkCore_ConsoleApp.Models;
using System.Linq;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var products = (from product in databaseContext.Products
where product.Name.ToLower().EndsWith("top 1".ToLower())
select product).ToList();
products.ForEach(product =>
{
Console.WriteLine("Id: " + product.Id);
Console.WriteLine("Name: " + product.Name);
Console.WriteLine("Price: " + product.Price);
Console.WriteLine("Quantity: " + product.Quantity);
Console.WriteLine("Description: " + product.Description);
Console.WriteLine("Status: " + product.Status);
Console.WriteLine("Photo: " + product.Photo);
Console.WriteLine("Created: " + product.Created.ToString("dd/MM/yyyy"));
Console.WriteLine("Category Id: " + product.CategoryId);
Console.WriteLine("---------------------");
});
}
}
}
}
Output
Id: 12
Name: Laptop 1
Price: 15
Quantity: 16
Description: Description 3
Status: True
Photo: photo4.gif
Created: 20/10/2021
Category Id: 2
---------------------