I have a method that retrieves timestamp intervals from db and it looks like this:
public IEnumerable<DateTime> GetCurrentsFlagTimestampPoints(DateTime startDate, DateTime endDate)
{
return currentsRepository.GetTimestampPoints(startDate, endDate).Select(timestamp => new DateTime(timestamp.Year, timestamp.Month, timestamp.Day)).Distinct();
}
But now I want to use it more in a more generic way, so i want to pass Func<>
that will describe resulting date format so once I want to return
new DateTime(timestamp.Year, timestamp.Month, timestamp.Day)
and once
new DateTime(timestamp.Year, timestamp.Month, timestamp.Day, timestamp.Hour)
I could do that using overloads of some sort but that would change the already existing structure of some methods.
I don't know how to tackle this, is it possible??
Edit:
It works nice without Expression so:
public IEnumerable<DateTime> GetCurrentsFlagTimestampPoints(DateTime startDate, DateTime endDate, Func<DateTime, DateTime> dateTimeExpr)
{
return currentsRepository.GetTimestampPoints(startDate, endDate).Select(dateTimeExpr).Distinct();
}
You need to pass a Expression<Func<DateTime,DateTime>>
as a param:
public IEnumerable<DateTime> GetCurrentsFlagTimestampPoints(DateTime startDate, DateTime endDate, Expression<Func<DateTime, DateTime>> dateTimeExpr)
{
return currentsRepository.GetTimestampPoints(startDate, endDate).Select(dateTimeExpr).Distinct();
}
Expression<Func<DateTime, Datetime>> f1 = timestamp => new DateTime(timestamp.Year, timestamp.Month, timestamp.Day);
Expression<Func<DateTime, Datetime>> f1 = timestamp => new DateTime(timestamp.Year, timestamp.Month, timestamp.Day, timestamp.Hour);
GetCurrentsFlagTimestampPoints(DateTime.Now, DateTime.Now, f1); // or f2
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments