C#“System.Linq.IQueryable”类型的IOrderedQueryable表达式“1”不能用于“System.Linq.IOrderedQueryable”类型的参数

nqwrtyyt  于 2023-05-20  发布在  C#
关注(0)|答案(2)|浏览(155)

我在“ThenBy”部分收到此错误

Expression of type 'System.Linq.IQueryable`1 cannot be used for parameter of type 'System.Linq.IOrderedQueryable
using Newtonsoft.Json.Linq;
using Common.Constant;
using System.Collections;
using System.Linq.Expressions;

namespace Common
{
    public static class LinqHelper
    {
        public static IOrderedQueryable<TSource> OrderByDefault<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, bool isInitialQueryable)
        {
            var ordered = source as IOrderedQueryable<TSource>;

            if (ordered == null || isInitialQueryable)
            {
                return source.OrderBy(keySelector); // <<-- no issue for this line. the system will get into here for the first column
            }
            else
            {
                return ordered.ThenBy(keySelector); // <<-- this line will prompt above error
            }
        }
    }
}

这个想法是,系统应该能够生成排序的代码。代码运行第一个“if语句”没有问题。但在“else语句”上失败

vi4fp9gy

vi4fp9gy1#

由于某些原因,检查总是返回true,即可查询对象总是有序的。不是100%确定为什么会这样。也许是查询提供程序,它从一开始就将它作为有序的可查询对象生成,然后再对其执行任何其他工作。
这个方法检查queryable是否以我测试时似乎有效的方式排序:

public static IOrderedQueryable<TSource> OrderByDefault<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, bool isInitialQueryable)
{
  if (source.Expression.Type != typeof(IOrderedQueryable<TSource>) || isInitialQueryable)
  {
    return source.OrderBy(keySelector);
  }
  else
  {
    return (source as IOrderedQueryable<TSource>).ThenBy(keySelector);
  }
}

我也尝试了一个在if语句中使用此语句的版本,但它总是在OrderBy部分结束:

if (!typeof(IOrderedQueryable<T>).IsAssignableFrom(source.Expression.Type) || isInitialQueryable)
btqmn9zl

btqmn9zl2#

根据Svyatoslav Danyliv的响应,我将参数从“this IQueryable source”更改为“IOrderedQueryable source”

public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, bool isInitialQueryable, bool isDescending)
        {
            if (isInitialQueryable)
            {
                if (isDescending)
                {
                    return source.OrderByDescending(keySelector);
                }
                else
                {
                    return source.OrderBy(keySelector);
                }
            }
            else
            {
                if (isDescending)
                {
                    return source.ThenByDescending(keySelector);
                }
                else
                {
                    return source.ThenBy(keySelector);
                }
            }
        }

下面是我如何使用该函数的示例

private IOrderedQueryable<Mt_User> PopulateSortingCondition(SortingInfo[] criteria, IQueryable<Mt_User> source)
        {
            var initialQuery = source as IOrderedQueryable<Mt_User>;
            IOrderedQueryable<Mt_User> result = null;
            var isResetCondition = true;
            var isUnhandledField = false;

            if (criteria != null && criteria.Any())
            {
                foreach (var field in criteria)
                {
                    isUnhandledField = false;

                    switch (field.Selector)
                    {
                        case "COMPANYDESCR":
                            result = initialQuery.OrderBy(Mt_User => (Mt_User.Account.CODE + " - " + Mt_User.Account.COMPANYNAME + " - " + Mt_User.Account.DESCR), isResetCondition, field.Desc);
                            break;
                        case "ACCESSRIGHT":
                            result = initialQuery.OrderBy(Mt_User => (Mt_User.Role.CODE), isResetCondition, field.Desc);
                            break;
                        case "USERNAME":
                            result = initialQuery.OrderBy(Mt_User => (Mt_User.Contact.NAME), isResetCondition, field.Desc);
                            break;
                        default:
                            isUnhandledField = true;
                            break;
                    }

                    if (!isUnhandledField)
                    {
                        initialQuery = result;
                        isResetCondition = false;
                    }
                }
            }

            if (result == null)
            {
                result = initialQuery
               .OrderBy(Mt_User => Mt_User.Contact.NAME)
                .ThenBy(Mt_User => Mt_User.Role.CODE)
                .ThenBy(Mt_User => Mt_User.Account.CODE + " - " + Mt_User.Account.COMPANYNAME + " - " + Mt_User.Account.DESCR);
            }

            return result;
        }

相关问题