Use Count with Lambda Expressions in Entity Framework Core

This project will demonstrate how to Use Count with 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();

            var count1 = db.Products.Count();
            Console.WriteLine("Count Products: " + count1);

            var count2 = db.Products.Count(p => p.Status == true);
            Console.WriteLine("Conditions in Count Products: " + count2);

            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