Single Method in LINQ MeThods in Entity Framework Core

Single method is used to get exactly one item to be returned by a query from a database using Entity Framework Core. Single method will throw an exception if the query does not return exactly one item.

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var product = databaseContext.Products.Single(product => product.Id == 9);
                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);
            }
        }
    }
}
Id: 9
Name: Tivi 1
Price: 10
Quantity: 15
Description: Description 1
Status: True
Photo: photo1.gif
Created: 20/10/2023
Category Id: 1