C# Count()不';在将IQueryable应用于Linq Dynamic、Spiring和#x27后,t工作;必须是可约节点';EF核心中的错误

C# Count()不';在将IQueryable应用于Linq Dynamic、Spiring和#x27后,t工作;必须是可约节点';EF核心中的错误,c#,linq,entity-framework-core,dynamicquery,dynamic-linq-core,C#,Linq,Entity Framework Core,Dynamicquery,Dynamic Linq Core,当param.SearchValue==string.EmptyCount()工作时。但在其他情况下,Count()或任何其他执行都不起作用,引发异常: 必须是可约节点 这是我的密码: public DataTableReturnValue<T> setParameter<T>(DataTableParameters param, IQueryable<T> query) where T : class { var columnNameLi

param.SearchValue==string.Empty
Count()
工作时。但在其他情况下,
Count()
或任何其他执行都不起作用,引发异常:

必须是可约节点

这是我的密码:

public DataTableReturnValue<T> setParameter<T>(DataTableParameters param, IQueryable<T> query) where T : class
    {
      var columnNameList = JsonConvert.DeserializeObject<List<ColumnName>>(param.columnName).Select(x => x.data).ToList();
      int totalrows = query.Count();
      Expression<Func<T, bool>> expressionCombine = DataTableExpressions.ContainsExp<T>(columnNameList.First(), param.SearchValue);
      if (param.SearchValue != string.Empty)
      {
        int i = 0;
        foreach (string ColumnNameItem in columnNameList)
        {
          if (i != 0)
          {
            var predicate = DataTableExpressions.ContainsExp<T>(ColumnNameItem, param.SearchValue);
            expressionCombine = DataTableExpressions.AppendExpression(expressionCombine, predicate);
          }
          i++;
        }
      }

      query = query.Where(expressionCombine);
      int totalrowsafterfiltering = query.Count();
      query = query.OrderBy(param.SortColumnName + " " + param.SortDirection);
      if (param.Length != -1)
        query = query.Skip(param.StartValue).Take(param.Length);
      return (new DataTableReturnValue<T> { data = query.ToList(), draw = param.Draw, recordsTotal = totalrows, recordsFiltered = totalrowsafterfiltering });
    }


 public class DataTableReturnValue<T> where T :class
  {
    public List<T> data{ get; set; }
    public int recordsTotal { get; set; }
    public int recordsFiltered { get; set; }
    public string draw { get; set; }
  }

public class DataTableParameters
  {
    public string SearchValue { get; set; }
    public int StartValue { get; set; }
    public int Length { get; set; }
    public string SortColumnName { get; set; }
    public string SortDirection { get; set; }
    public string Draw { get; set; }
    public int application_id { get; set; }
    public string columnName { get; set; }
    public int user_id { get; set; }
  }
  public static class DataTableExpressions
  {
    public static Expression<Func<T, bool>> ContainsExp<T>(string propertyName, string contains)
    {
      var propertyType = typeof(T).GetProperty(propertyName).PropertyType;
      var parameterExp = Expression.Parameter(typeof(T), "type");
      Expression propertyExp = Expression.Property(parameterExp, propertyName);
      if (propertyType == typeof(int) || propertyType == typeof(int?))
      {
        propertyExp = Expression.Convert(propertyExp, typeof(double?));
        var stringConvertMethod = typeof(SqlFunctions).GetMethod("StringConvert", new[] { typeof(double?) });
        propertyExp = Expression.Call(stringConvertMethod, propertyExp);
      }
      else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
      {
        propertyExp = Expression.Convert(propertyExp, typeof(DateTime?));
        var stringConvertMethod = typeof(SqlFunctions).GetMethod("StringConvert", new[] { typeof(DateTime?) });
        propertyExp = Expression.Call(stringConvertMethod, propertyExp);
      }
      var method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
      var someValue = Expression.Constant(contains, typeof(string));
      var containsMethodExp = Expression.Call(propertyExp, method, someValue);
      return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);

    }

    public static Expression<Func<T, bool>> AppendExpression<T>(Expression<Func<T, Boolean>> left, Expression<Func<T, Boolean>> right)
    {
      Expression<Func<T, bool>> result;
      result = OrElse(left, right);
      return result;
    }

    public static Expression<Func<T, bool>> OrElse<T>(Expression<Func<T, Boolean>> left, Expression<Func<T, Boolean>> right)
    {
      Expression<Func<T, Boolean>> combined = Expression.Lambda<Func<T, Boolean>>(
          Expression.OrElse(
              left.Body,
              new ExpressionParameterReplacer(right.Parameters, left.Parameters).Visit(right.Body)
              ), left.Parameters);

      return combined;
    }
  }

  public class ExpressionParameterReplacer : ExpressionVisitor
  {
    private IDictionary<ParameterExpression, ParameterExpression> ParameterReplacements { get; set; }

    public ExpressionParameterReplacer(IList<ParameterExpression> fromParameters, IList<ParameterExpression> toParameters)
    {
      ParameterReplacements = new Dictionary<ParameterExpression, ParameterExpression>();

      for (int i = 0; i != fromParameters.Count && i != toParameters.Count; i++)
      { ParameterReplacements.Add(fromParameters[i], toParameters[i]); }
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
      ParameterExpression replacement;

      if (ParameterReplacements.TryGetValue(node, out replacement))
      { node = replacement; }

      return base.VisitParameter(node);
    }
  }