I have these two methods:
private static OpeningDay GetUsallyOpeningHour(List<OpeningDay> openingHoursDaily, DayOfWeek dayOfWeek)
{
var listOfSpecificDayOfWeek = openingHoursDaily.Where(x => x.DateTime.DayOfWeek == dayOfWeek);
if(listOfSpecificDayOfWeek.Any())
return listOfSpecificDayOfWeek.GroupBy(x => x.From).OrderByDescending(x => x.Count()).ThenByDescending(x => x.Key).First().First();
return new OpeningDay { From = new TimeSpan(0, 0, 0), To = new TimeSpan(0, 0, 0), DateTime = DateTime.Now };
}
private static OpeningDay GetUsallyClosingHour(List<OpeningDay> openingHoursDaily, DayOfWeek dayOfWeek)
{
var listOfSpecificDayOfWeek = openingHoursDaily.Where(x => x.DateTime.DayOfWeek == dayOfWeek);
if (listOfSpecificDayOfWeek.Any())
return listOfSpecificDayOfWeek.GroupBy(x => x.To).OrderByDescending(x => x.Count()).ThenByDescending(x => x.Key).First().First();
return new OpeningDay { From = new TimeSpan(0, 0, 0), To = new TimeSpan(0, 0, 0), DateTime = DateTime.Now };
}
The only difference between them is that one is grouping by x.From
and other by x.To
, and I was wondering how I can improve this code.
Basically, I can a create method which takes lambda, but how long? At first I was thinking to pass this:
GroupBy(x => x.To).OrderByDescending(x => x.Count()).ThenByDescending(x => x.Key)
as a lambda parameter. But this would take me ending with function that takes a lambda and then returns sth like lambda.First().First()
.
What do you recommend? Are there some other improvements?