1 回答
TA贡献1815条经验 获得超6个赞
简单但具体
您可以添加这样的扩展方法:
public static class DateTimeExtensions
{
public static bool IsBetween(this DateTime thisDateTime, DateTime start, DateTime end)
{
return thisDateTime >= start && thisDateTime <= end;
}
}
您可以单独进行单元测试。
然后您可以在要检查的任何日期时间字段上使用它。例如:
var start = new DateTime(2017, 1, 1);
var end = new DateTime(2017, 12, 31, 23, 59, 59);
IList<Contract> contracts = new List<Contract>(); // or anything enumerable
var contractsSignedBetween = contracts.Where(x => x.SignDate.IsBetween(start, end));
var contractsReleasedBetween = contracts.Where(x => x.ReleaseDate.IsBetween(start, end));
(请注意我如何将开始日期时间设置为 00:00:00 时间,将结束日期时间设置为 23:59:59 时间[也可以包括毫秒],以便包括最后一天的时间。)
使其可重用
如果你发现自己需要做很多事情,你可以做一个扩展
public static class EnumerableContractsExtensions
{
public static IEnumerable<Contract> SignedBetween(this IEnumerable<Contract> contracts, DateTime start, DateTime end)
{
return contracts.Where(x => x.SignDate.IsBetween(start, end));
}
}
并像这样使用它
var contractsSignedBetween = contracts.SignedBetween(start, end);
也可以单独进行单元测试。
更灵活但更具体
使用表达式说出您想要的日期...
public static class EnumerableContractsExtensions
{
public static IEnumerable<Contract> Between(this IEnumerable<Contract> contracts, Func<Contract, DateTime> selector, DateTime start, DateTime end)
{
return contracts.Where(x => selector(x).IsBetween(start, end));
}
}
然后做:
var contractsSignedBetween = contracts.Between(x => x.SignDate, start, end);
var contractsReleasedBetween = contracts.Between(x => x.ReleaseDate, start, end);
灵活通用
全力以赴并通用(尽管您不能将其设为扩展方法,因为它是通用的):
public static class EnumerableExtensions
{
public static IEnumerable<T> Between<T>(IEnumerable<T> items, Func<T, DateTime> selector, DateTime start, DateTime end)
{
return items.Where(x => selector(x).IsBetween(start, end));
}
}
同样,这本身就是可测试的,可以像这样使用:
IList<Contract> contracts = new List<Contract>();
IList<PersonalCheck> personalChecks = new List<PersonalCheck>();
var contractsSignedBetween = EnumerableExtensions.Between(contracts, x => x.SignDate, start, end);
var checksSignedBetween = EnumerableExtensions.Between(personalChecks, x => x.SignDate, start, end);
使其可查询
为了使该方法起作用,IQueryable需要转向表达式树,因为 LINQ to Entities 不知道如何将方法转换为 SQL。
public static IQueryable<TSource> Between<TSource, TKey>(
this IQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector,
TKey low,
TKey high)
where TKey : IComparable<TKey>
{
Expression key = keySelector.Body;
Expression lowerBound = Expression.LessThanOrEqual(Expression.Constant(low), key);
Expression upperBound = Expression.LessThanOrEqual(key, Expression.Constant(high));
Expression and = Expression.AndAlso(lowerBound, upperBound);
Expression<Func<TSource, bool>> lambda =
Expression.Lambda<Func<TSource, bool>>(and, keySelector.Parameters);
return source.Where(lambda);
}
仍然会像这样使用:
var contractsSignedBetween = contracts.Between(x => x.SignDate, start, end);
这也适用于DateTimes以外的事物。希望这可以帮助。
- 1 回答
- 0 关注
- 159 浏览
添加回答
举报