Entity Framework Core allows compare date part of datetime/timestamp column in your database.
using EntityFrameworkCore_ConsoleApp.Models;
using System.Globalization;
using System.Linq;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var startDate = DateTime.ParseExact("20/10/2023", "dd/MM/yyyy", CultureInfo.InvariantCulture);
var endDate = DateTime.ParseExact("20/12/2023", "dd/MM/yyyy", CultureInfo.InvariantCulture);
var products = (from product in databaseContext.Products
where product.Created >= startDate && product.Created <= endDate
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: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------
Id: 15
Name: Computer 2
Price: 43
Quantity: 11
Description: Description 6
Status: False
Photo: photo7.gif
Created: 20/11/2023
Category Id: 3
---------------------
Find by Year from datetime column as below:
using EntityFrameworkCore_ConsoleApp.Models;
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.Created.Year == 2023
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: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------
Id: 15
Name: Computer 2
Price: 43
Quantity: 11
Description: Description 6
Status: False
Photo: photo7.gif
Created: 20/11/2023
Category Id: 3
---------------------
Id: 16
Name: Computer 3
Price: 19
Quantity: 18
Description: Description 7
Status: True
Photo: photo8.gif
Created: 20/05/2023
Category Id: 3
---------------------
Find by Year and Month from datetime column as below:
using EntityFrameworkCore_ConsoleApp.Models;
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.Created.Year == 2023 && product.Created.Month == 12
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: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------
Find by Year and Month and Day from datetime column as below:
using EntityFrameworkCore_ConsoleApp.Models;
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.Created.Year == 2023 && product.Created.Month == 12 && product.Created.Day == 20
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: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------