First method is used when you expect one or more items to be returned by a query but you only want to access the first item in your code. This will throw an exception if the query does not return at least 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.First();
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);
}
}
}
}
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
You can also add condition to First method as below:
using EntityFrameworkCore_ConsoleApp.Models;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var product = databaseContext.Products.First(product => product.Status == false);
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);
}
}
}
}
Output
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 First 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 product = databaseContext.Products.Where(product => product.Status).First();
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);
}
}
}
}
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
You can use combination of First 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 product = databaseContext.Products.Where(product => product.Status).OrderBy(product => product.Price).First();
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);
}
}
}
}
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
You can use combination of First 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 product = databaseContext.Products.Where(product => product.Status).OrderBy(product => product.Price).Select(product => new
{
Id = product.Id,
Name = product.Name,
Price = product.Price
}).First();
Console.WriteLine("Id: " + product.Id);
Console.WriteLine("Name: " + product.Name);
Console.WriteLine("Price: " + product.Price);
}
}
}
}
Output
Id: 9
Name: Tivi 1
Price: 10