Use DateTime in Lambda Expressions in Entity Framework Core

This project will demonstrate how to Use DateTime in Lambda Expressions in Entity Framework Core

  • LearnEntityFrameworkCore
    • Dependencies
      • Nuget
        • Microsoft.EntityFrameworkCore
        • Microsoft.EntityFrameworkCore.SqlServer
        • Microsoft.EntityFrameworkCore.Proxies
        • Microsoft.EntityFrameworkCore.Tools
        • Microsoft.EntityFrameworkCore.Design
    • Models
      • Product.cs
      • Category.cs
      • LearnEntityFrameworkCoreDB.cs
    • appsettings.json
    • Program.cs
using System;
using System.Linq;
using LearnEntityFrameworkCoreWithRealApps.Models;

namespace LearnEntityFrameworkCoreWithRealApps
{
    class Program
    {
        static void Main(string[] args)
        {
            var db = new LearnEntityFrameworkCoreDB();

            Console.WriteLine("Use Year, Month and Day in DateTime");
            var productList1 = db.Products.Where(p => p.CreationDate.Year == 2017 && p.CreationDate.Month == 12 && p.CreationDate.Day == 20).ToList();
            foreach (var product in productList1)
            {
                Console.WriteLine("Id: " + product.Id);
                Console.WriteLine("Name: " + product.Name);
                Console.WriteLine("Price: " + product.Price);
                Console.WriteLine("Quantity: " + product.Quantity);
                Console.WriteLine("Status: " + product.Status);
                Console.WriteLine("Creation Date: " + product.CreationDate.ToString("MM/dd/yyyy"));
                Console.WriteLine("Category Id: " + product.Category.Id);
                Console.WriteLine("Category Name: " + product.Category.Name);
                Console.WriteLine("==========================");
            }

            Console.WriteLine("\nCompare Date");
            var productList2 = db.Products.Where(p => p.CreationDate >= new DateTime(2017, 12, 15) && p.CreationDate <= new DateTime(2017, 12, 25)).ToList();
            foreach (var product in productList2)
            {
                Console.WriteLine("Id: " + product.Id);
                Console.WriteLine("Name: " + product.Name);
                Console.WriteLine("Price: " + product.Price);
                Console.WriteLine("Quantity: " + product.Quantity);
                Console.WriteLine("Status: " + product.Status);
                Console.WriteLine("Creation Date: " + product.CreationDate.ToString("MM/dd/yyyy"));
                Console.WriteLine("Category Id: " + product.Category.Id);
                Console.WriteLine("Category Name: " + product.Category.Name);
                Console.WriteLine("==========================");
            }

            Console.ReadLine();
        }
    }
}
{
    "ConnectionStrings": {
        "DefaultConnection": "Server=.;Database=LearnEntityFrameworkCore;user id=sa;password=123456"
    }
}
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace LearnEntityFrameworkCoreWithRealApps.Models
{
    public partial class LearnEntityFrameworkCoreDB : DbContext
    {
        public virtual DbSet<Category> Categories { get; set; }
        public virtual DbSet<Product> Products { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                var builder = new ConfigurationBuilder()
                                    .SetBasePath(Directory.GetCurrentDirectory())
                                    .AddJsonFile("appsettings.json");
                var configuration = builder.Build();
                optionsBuilder.UseLazyLoadingProxies()
                                .UseSqlServer(configuration["ConnectionStrings:DefaultConnection"]);
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>(entity =>
            {
                entity.HasOne(d => d.Category)
                    .WithMany(p => p.Products)
                    .HasForeignKey(d => d.CategoryId)
                    .HasConstraintName("FK_Product_Category");
            });
        }
    }
}
using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace LearnEntityFrameworkCoreWithRealApps.Models
{
    [Table("Product")]
    public partial class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public int Quantity { get; set; }
        public DateTime CreationDate { get; set; }
        public bool Status { get; set; }
        public int CategoryId { get; set; }

        public virtual Category Category { get; set; }
    }
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace LearnEntityFrameworkCoreWithRealApps.Models
{
    [Table("Category")]
    public partial class Category
    {
        public Category()
        {
            Products = new HashSet<Product>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }
}

Screenshots