Distinct method is used to return the distinct elements from a column in Entity Framework Core.
using EntityFrameworkCore_ConsoleApp.Models;
namespace EntityFrameworkCore_ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var databaseContext = new DatabaseContext())
{
var categoryIds = databaseContext.Products.Select(product => product.CategoryId).Distinct().ToList();
categoryIds.ForEach(categoryId =>
{
Console.WriteLine("Category Id: " + categoryId);
Console.WriteLine("---------------------");
});
}
}
}
}
Output
Category Id: 1
---------------------
Category Id: 2
---------------------
Category Id: 3
---------------------