Web API + ODataQueryOptions + $ top或$ skip导致SqlException

时间:2013-08-02 00:37:25

标签: asp.net-mvc asp.net-web-api

此示例已针对此示例进行了简化。

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

[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());
}

但是这对我来说并不完全有用,因为它有点hackish,我不能使用已经给我一个IQueryable的服务方法。

另外需要注意的是,如果将Logging方法转换为IQueryable,一切都能正常工作。但是我需要使用查询返回Count,所以我必须返回一个PageResult。

1 个答案:

答案 0 :(得分:2)

这是我正在使用的解决方法。我只应用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);
}