Apply Distinct in specification query
mhd-ahmadi opened this issue · comments
Mohammad Ahmadi commented
I have problem with this query I have multiple repeated data from query!
public class DataSetGetByFilterProjectsResultSpec : Specification<DataSet, ProjectViewDto>
{
public DataSetGetByFilterProjectsResultSpec(DataSetFilter filter)
{
Query
.ApplyFiltering(filter);
Query
.Where(w => w.ProjectId.HasValue)
.Include(c => c.Project)
;
Query
.Select(ds => new ProjectViewDto(ds.ProjectId!.Value, ds.Project!.Name));
}
}
var availableProjects = await _repository.ListAsync(new DataSetGetByFilterProjectsResultSpec(filter), ct);
Response.Projects = availableProjects.Distinct().ToList();
Fati Iseni commented
Hi @mhd-ahmadi,
I don't think this has to do with this library. You're fetching data from the database, and then applying Distinct
to an in-memory collection. The reason it's not working is because the Distinct
LINQ method uses the Equals
method to compare the objects. If your model ProjectViewDto
is a class, then of course it won't work. You should override the Equals
method, or more simply just use record
instead of class
.