FirstOrDefault method will return the first item or return the default value (which is null in case the given type is a reference type) when there is no item from a 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 productInfo = (from product in databaseContext.Products
select product).FirstOrDefault();
Console.WriteLine("Id: " + productInfo.Id);
Console.WriteLine("Name: " + productInfo.Name);
Console.WriteLine("Price: " + productInfo.Price);
Console.WriteLine("Quantity: " + productInfo.Quantity);
Console.WriteLine("Description: " + productInfo.Description);
Console.WriteLine("Status: " + productInfo.Status);
Console.WriteLine("Photo: " + productInfo.Photo);
Console.WriteLine("Created: " + productInfo.Created.ToString("dd/MM/yyyy"));
Console.WriteLine("Category Id: " + productInfo.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 FirstOrDefault method as below:
using EntityFrameworkCore_ConsoleApp.Models;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var productInfo = (from product in databaseContext.Products
select product).FirstOrDefault(product => product.Status == false);
Console.WriteLine("Id: " + productInfo.Id);
Console.WriteLine("Name: " + productInfo.Name);
Console.WriteLine("Price: " + productInfo.Price);
Console.WriteLine("Quantity: " + productInfo.Quantity);
Console.WriteLine("Description: " + productInfo.Description);
Console.WriteLine("Status: " + productInfo.Status);
Console.WriteLine("Photo: " + productInfo.Photo);
Console.WriteLine("Created: " + productInfo.Created.ToString("dd/MM/yyyy"));
Console.WriteLine("Category Id: " + productInfo.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 FirstOrDefault method and Where as below:
using EntityFrameworkCore_ConsoleApp.Models;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var productInfo = (from product in databaseContext.Products
where product.Status == true
select product).FirstOrDefault();
Console.WriteLine("Id: " + productInfo.Id);
Console.WriteLine("Name: " + productInfo.Name);
Console.WriteLine("Price: " + productInfo.Price);
Console.WriteLine("Quantity: " + productInfo.Quantity);
Console.WriteLine("Description: " + productInfo.Description);
Console.WriteLine("Status: " + productInfo.Status);
Console.WriteLine("Photo: " + productInfo.Photo);
Console.WriteLine("Created: " + productInfo.Created.ToString("dd/MM/yyyy"));
Console.WriteLine("Category Id: " + productInfo.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 FirstOrDefault method and Where and OrderBy as below:
using EntityFrameworkCore_ConsoleApp.Models;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var productInfo = (from product in databaseContext.Products
where product.Status == true
orderby product.Price
select product).FirstOrDefault();
Console.WriteLine("Id: " + productInfo.Id);
Console.WriteLine("Name: " + productInfo.Name);
Console.WriteLine("Price: " + productInfo.Price);
Console.WriteLine("Quantity: " + productInfo.Quantity);
Console.WriteLine("Description: " + productInfo.Description);
Console.WriteLine("Status: " + productInfo.Status);
Console.WriteLine("Photo: " + productInfo.Photo);
Console.WriteLine("Created: " + productInfo.Created.ToString("dd/MM/yyyy"));
Console.WriteLine("Category Id: " + productInfo.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 FirstOrDefault method and Where and OrderBy and Select as below:
using EntityFrameworkCore_ConsoleApp.Models;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var productInfo = (from product in databaseContext.Products
where product.Status == true
orderby product.Price
select new
{
Id = product.Id,
Name = product.Name,
Price = product.Price
}).FirstOrDefault();
Console.WriteLine("Id: " + productInfo.Id);
Console.WriteLine("Name: " + productInfo.Name);
Console.WriteLine("Price: " + productInfo.Price);
}
}
}
}
Output
Id: 9
Name: Tivi 1
Price: 10