62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
|
using Ardalis.Specification;
|
|||
|
using Ardalis.Specification.EntityFrameworkCore;
|
|||
|
using DD.Persistence.Database.Entity;
|
|||
|
using DD.Persistence.Database.EntityAbstractions;
|
|||
|
using DD.Persistence.Models.Common;
|
|||
|
|
|||
|
namespace DD.Persistence.Database.Postgres.Repositories;
|
|||
|
public class MyPartialEvaluator : IEvaluator
|
|||
|
{
|
|||
|
private MyPartialEvaluator() { }
|
|||
|
public static MyPartialEvaluator Instance { get; } = new MyPartialEvaluator();
|
|||
|
|
|||
|
public bool IsCriteriaEvaluator { get; } = true;
|
|||
|
|
|||
|
public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class
|
|||
|
{
|
|||
|
|
|||
|
// Проверяем, есть ли свойство Timestamp в типе T
|
|||
|
var timestampProperty = typeof(T).GetProperty("Timestamp");
|
|||
|
|
|||
|
if (timestampProperty != null && timestampProperty.PropertyType == typeof(DateTimeOffset))
|
|||
|
{
|
|||
|
// Если свойство существует и имеет тип DateTime, выполняем группировку и выборку
|
|||
|
var t = query
|
|||
|
.GroupBy(x => 1)
|
|||
|
.Select(group => new
|
|||
|
{
|
|||
|
Min = group.Min(e => (DateTimeOffset)timestampProperty.GetValue(e)),
|
|||
|
Max = group.Max(e => (DateTimeOffset)timestampProperty.GetValue(e)),
|
|||
|
})
|
|||
|
.AsQueryable() as IQueryable<T>;
|
|||
|
return t;
|
|||
|
}
|
|||
|
|
|||
|
return query;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public class Test
|
|||
|
{
|
|||
|
public Test()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
public DateTimeOffset Min { get; set; }
|
|||
|
public DateTimeOffset Max { get; set; }
|
|||
|
}
|
|||
|
|
|||
|
public class MySpecificationEvaluator : SpecificationEvaluator
|
|||
|
{
|
|||
|
public static MySpecificationEvaluator GroupBy { get; } = new MySpecificationEvaluator();
|
|||
|
|
|||
|
private MySpecificationEvaluator() : base()
|
|||
|
{
|
|||
|
Evaluators.Add(MyPartialEvaluator.Instance);
|
|||
|
}
|
|||
|
}
|