Web API + ODataQueryOptions + $top or $skip is causing a Sql



本例简化了此代码。

查询实际上是从服务返回的,这就是为什么我更喜欢用这种方式编写方法。

[HttpGet]
public PageResult<ExceptionLog> Logging(ODataQueryOptions<ExceptionLog> options)
{
    var query = from o in _exceptionLoggingService.entities.ExceptionDatas
                select new ExceptionLog {
                    ExceptionDataId = o.ExceptionDataId,
                    SiteId = o.SiteId,
                    ExceptionDateTime = o.ExceptionDateTime,
                    StatusCode = o.StatusCode,
                    Url = o.Url,
                    ExceptionType = o.ExceptionType,
                    ExceptionMessage = o.ExceptionMessage,
                    Exception = o.Exception,
                    RequestData = o.RequestData
                };
    var results = options.ApplyTo(query) as IEnumerable<ExceptionLog>;
    var count = results.LongCount();
    return new PageResult<ExceptionLog>(results, Request.GetNextPageLink(), count);
}

上面的代码在"results.LongCount()"上出现错误,并出现以下异常:

SqlException: The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

我在尝试分页时似乎遇到了一个异常,比如"$top=2"。如果我的查询字符串是这样的"$filter=ExceptionDataId gt 100",一切都很好。

由于ExceptionData(实体)与ExceptionLog(业务模型)匹配,我可以做一些类似的工作:

[HttpGet]
public PageResult<ExceptionLog> Logging(ODataQueryOptions<ExceptionData> options)
{
    var query = from o in _exceptionLoggingService.entities.ExceptionDatas
                orderby o.ExceptionDateTime descending
                select o;
    var results = from o in options.ApplyTo(query) as IEnumerable<ExceptionData>
                    select new ExceptionLog {
                        ExceptionDataId = o.ExceptionDataId,
                        SiteId = o.SiteId,
                        ExceptionDateTime = o.ExceptionDateTime,
                        StatusCode = o.StatusCode,
                        Url = o.Url,
                        ExceptionType = o.ExceptionType,
                        ExceptionMessage = o.ExceptionMessage,
                        Exception = o.Exception,
                        RequestData = o.RequestData
                    };
    return new PageResult<ExceptionLog>(results, Request.GetNextPageLink(), results.LongCount());
}

但这对我来说并不完全有效,因为它有点烦人,而且我不能使用该服务的方法,因为它已经给了我一个IQueryable。

另一件需要注意的事情是,如果将Logging方法转换为IQueryable,则一切正常。但是我需要用查询返回Count,所以我必须返回一个PageResult。

这是我正在使用的解决方法。我只应用ODataQueryOptions中的筛选器,并手动应用Top和Skip。

首先我创建了一些扩展方法:

using System;
using System.Collections.Generic;
using System.Linq;
namespace System.Web.Http.OData.Query
{
    public static class ODataQuerySettingsExtensions
    {
        public static IEnumerable<T> ApplyFilter<T>(this IQueryable<T> query, ODataQueryOptions<T> options)
        {
            if (options.Filter == null)
            {
                return query;
            }
            return options.Filter.ApplyTo(query, new ODataQuerySettings()) as IEnumerable<T>;
        }
        public static IEnumerable<T> ApplyTopAndTake<T>(this IEnumerable<T> query, ODataQueryOptions<T> options)
        {
            IEnumerable<T> value = query;
            if (options.Top != null)
            {
                value = value.Take(options.Top.Value);
            }
            if (options.Skip != null)
            {
                value = value.Skip(options.Skip.Value);
            }
            return value;
        }
    }
}

现在我的方法是这样的:

[HttpGet]
public PageResult<ExceptionLog> Logging(ODataQueryOptions<ExceptionLog> options)
{
    // GetLogs returns an IQueryable<ExceptionLog> as seen in Question above.
    var query = _exceptionLoggingService.GetLogs()
                                        .ApplyFilter(options);
    var count = query.Count();
    var results = query.ApplyTopAndTake(options);
    return new PageResult<ExceptionLog>(results, Request.GetNextPageLink(), count);
}

最新更新