entity-framework-rcerda

Entity Framework: Get Distinct Result Set on Multiple Columns with Order By

I am creating a report based off of the following result set:

ef-distinct-multi-column

I wanted to get a unique set of weeks from the control group, but I also wanted to retrieve whether the week is an actual vs. a forecast.

Turns out it’s pretty easy:

public List<WeeklyUtilizationHistoryAndForecast> ReportSummary = businessService.GetWeeklyUtilizationHistoryAndForecast(isCache, ParamUserId, ParamStartWeek, ParamEndWeek, ParamBusinessUnitId, ParamIncludeTimeOff);

public List<WeeklyUtilizationHistoryAndForecast> Weeks = ReportSummary.OrderBy(m => m.WeekDisplay).DistinctBy(m => new { m.WeekDisplay, m.IsActual }).ToList();

Leave a Reply