NejCommon.NET/Models/DateTimeFrame.cs
2024-09-15 19:47:20 +02:00

35 lines
1.0 KiB
C#

using System.Linq.Expressions;
using NejCommon.Utils;
namespace NejCommon.Models;
public class DateTimeFrame
{
public DateTimeFrame(){
}
public DateTimeFrame(DateTime fromDate, DateTime toDate)
{
FromDate = fromDate;
ToDate = toDate;
}
public DateTime FromDate { get; set; } = DateTime.MinValue;
public DateTime ToDate { get; set; } = DateTime.MaxValue;
public override string ToString()
{
var from = (FromDate == DateTime.MinValue) ? "" : ("-" + FromDate.ToShortDateString());
var to = (ToDate == DateTime.MaxValue) ? "" : ("-" + ToDate.ToShortDateString());
return $"{from}{to}";
}
public static Expression<Func<TType, bool>> Conditional<TType>(Expression<Func<TType, DateTime>> dateAcessor, DateTimeFrame frame)
{
return e => dateAcessor.Compile()(e) >= frame.FromDate && dateAcessor.Compile()(e) <= frame.ToDate;
}
public DateOnlyFrame ToDateOnlyFrame()
{
return new DateOnlyFrame(FromDate.ToDateOnly(), ToDate.ToDateOnly());
}
}