我已經創建了下面的代碼更改日期過濾器的定義方式:
/// <summary>
/// Maps DateTime related filters.
/// * IsEqualTo filter is converted to ('x-date' is GreaterThanOrEqualTo 'SearchDate') AND ('x-date' is LessThan 'SearchDate' + 1 Day)
/// * IsNotEqualTo filter is converted to ('x-date' LessThan 'SearchDate') OR ('x-date' is GreaterThan 'SearchDate' + 1 Day) OR ('x-date' is NULL)
/// * IsLessThanOrEqualTo filter is converted to ('x-date' is LessThan 'SearchDate' + 1 Day)
/// </summary>
/// <param name="copyOfOriginalFilters">A copy from the filters.</param>
/// <param name="newFilters">The new filters</param>
protected void MapDateFilter(ReadOnlyCollection<IFilterDescriptor> copyOfOriginalFilters, IList<IFilterDescriptor> newFilters)
{
newFilters.Clear();
foreach (var currentFilter in copyOfOriginalFilters)
{
Type t = currentFilter.GetType();
if (t == typeof(FilterDescriptor))
{
var filter = currentFilter as FilterDescriptor;
if (filter.ConvertedValue.GetType() == typeof(DateTime))
{
DateTime datePart = ((DateTime)filter.ConvertedValue).Date;
if (filter.Operator == FilterOperator.IsEqualTo)
{
var compositeFilter = new CompositeFilterDescriptor();
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsGreaterThanOrEqualTo, datePart));
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsLessThan, datePart.AddDays(1)));
compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.And;
newFilters.Add(compositeFilter);
}
else if (filter.Operator == FilterOperator.IsNotEqualTo)
{
var compositeFilter = new CompositeFilterDescriptor();
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsLessThan, datePart));
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsGreaterThan, datePart.AddDays(1)));
compositeFilter.FilterDescriptors.Add(new FilterDescriptor(filter.Member, FilterOperator.IsEqualTo, null));
compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
newFilters.Add(compositeFilter);
}
else if (filter.Operator == FilterOperator.IsLessThanOrEqualTo)
{
newFilters.Add(new FilterDescriptor(filter.Member, FilterOperator.IsLessThan, datePart.AddDays(1)));
}
else
{
newFilters.Add(filter);
}
}
else
{
newFilters.Add(filter);
}
}
else if (t == typeof(CompositeFilterDescriptor))
{
var filter = currentFilter as CompositeFilterDescriptor;
int numberOfDateFilters = filter.FilterDescriptors.Where(fd =>
fd is FilterDescriptor &&
(fd as FilterDescriptor).ConvertedValue != null &&
(fd as FilterDescriptor).ConvertedValue.GetType() == typeof(DateTime))
.Count();
if (numberOfDateFilters == 2)
{
var firstFilter = filter.FilterDescriptors[0] as FilterDescriptor;
firstFilter.Value = ((DateTime)firstFilter.ConvertedValue).Date;
var secondFilter = filter.FilterDescriptors[1] as FilterDescriptor;
secondFilter.Value = ((DateTime)secondFilter.ConvertedValue).Date;
}
else
{
MapDateFilter(new List<IFilterDescriptor>(filter.FilterDescriptors).AsReadOnly(), filter.FilterDescriptors);
}
newFilters.Add(filter);
}
}
}
這可以用於像:
protected void MapGridCommand(GridCommand command)
{
this.MapDateFilter(new List<IFilterDescriptor>(command.FilterDescriptors).AsReadOnly(), command.FilterDescriptors);
}
scartag,感謝答覆。 CreatedOn字段是可以爲空的日期時間,所以我使用了col.Bound(m => m.CreatedOn.Value.Date),正如您所建議的那樣。但是,當我嘗試運行該應用程序時,出現「System.InvalidOperationException:Nullable對象必須有值」。 – Jatin
我正在編輯以處理場景。 – scartag
@Nirvan我完成了編輯。你可以試試看。 – scartag