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