我已经定义了自己的结构,它代表DateTime
和TimeZoneInfo
,所以我可以在保留有关时区的信息的同时使用UTC时间。Web Api自定义类型的OData查询
我想用OData查询获得这些对象,但是当我尝试在此类属性上使用$orderby
时失败。当我询问$orderBy=Timestamp/Value/UniversalTime
时我能得到结果,但我只想使用$orderBy=Timestamp
有没有可能使用此类型订购集合?
public struct DateTimeWithZone : IComparable<DateTime>, IComparable<DateTimeWithZone>, IFormattable
{
private readonly DateTime _utcDateTime;
private readonly TimeZoneInfo _timeZone;
public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone)
{
if (timeZone == null)
{
throw new NoNullAllowedException(nameof(timeZone));
}
_utcDateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
_timeZone = timeZone;
}
...
}
有了这样定义的模型:
public class ClientViewModel
{
public string Name { get; set; }
public DateTimeWithZone? Timestamp { get; set; }
}
,这是它是如何使用:
public IHttpActionResult GetAll(ODataQueryOptions<ClientViewModel> options)
{
var fromService = _clientsClient.GetAllClients().MapTo<ClientViewModel>(MappingStrategy).AsQueryable();
var totalCount = fromService.Count();
var results = options.ApplyTo(fromService); // <-- Fails here
return Ok(new PageResult<ClientViewModel>(
results as IEnumerable<ClientViewModel>,
Request.ODataProperties().NextLink,
totalCount));
}
与The $orderby expression must evaluate to a single value of primitive type.