Home > Mobile >  EF Core DistinctBy
EF Core DistinctBy

Time:08-24

I am trying to find an equivalent Linq Query to run the below code in SQL Database using EF Core.

  var result =  Logs.Where(x => x.ProjectCode.Equals(projectCode))
    .OrderByDescending(x => x.SubmittedDate).DistinctBy(y => new { y.GeographyCode, y.DataSetId })

The DistinctBy operation is creating an issue while translating query string from context. I am not sure how to write an equivalent LINQ operation that is compatible with EF Core which takes 2 distinct properties.

Is there a way to do this? I want to run this query on the SQL server end.

CodePudding user response:

try GroupBy

var result =  Logs.Where(x => x.ProjectCode.Equals(projectCode))
                  .OrderByDescending(x => x.SubmittedDate)
                  .GroupBy(y => new { y.GeographyCode, y.DataSetId })
                  .Select(g => g.First())
                  .ToList();
  • Related