Take Method in LINQ MeThods in Entity Framework Core

Use Take method allows to fetch the first “n” number of elements from database using Entity Framework Core.

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var products = databaseContext.Products.Take(2).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("---------------------");
                });
            }
        }
    }
}
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
---------------------

You can use combination of Take method and Where method as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var products = databaseContext.Products.Where(product => product.Status == true).Take(2).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("---------------------");
                });
            }
        }
    }
}
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: 12
Name: Laptop 1
Price: 15
Quantity: 16
Description: Description 3
Status: True
Photo: photo4.gif
Created: 20/10/2021
Category Id: 2
---------------------

You can use combination of Take method and Where method and OrderBy method as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var products = databaseContext.Products.Where(product => product.Status == true).OrderByDescending(product => product.Price).Take(2).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("---------------------");
                });
            }
        }
    }
}
Id: 16
Name: Computer 3
Price: 19
Quantity: 18
Description: Description 7
Status: True
Photo: photo8.gif
Created: 20/05/2023
Category Id: 3
---------------------
Id: 14
Name: Computer 1
Price: 17
Quantity: 34
Description: Description 5
Status: True
Photo: photo6.gif
Created: 20/12/2023
Category Id: 3
---------------------

You can use combination of Take method and Where method and OrderBy method and Select method as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var products = databaseContext.Products.Where(product => product.Status == true).OrderByDescending(product => product.Price).Take(2).Select(product => new
                {
                    Id = product.Id,
                    Name = product.Name,
                    Price = product.Price
                }).ToList();
                products.ForEach(product =>
                {
                    Console.WriteLine("Id: " + product.Id);
                    Console.WriteLine("Name: " + product.Name);
                    Console.WriteLine("Price: " + product.Price);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Id: 16
Name: Computer 3
Price: 19
---------------------
Id: 14
Name: Computer 1
Price: 17
---------------------