LINQ TO SQL, Dynamic query with DATE type fields

前端 未结 4 912
一整个雨季
一整个雨季 2020-12-09 06:22

I\'m building a query with the LINQ dynamic library so I don\'t know how many potential parameters will I have and I get an error when trying to query DATE type fields:

4条回答
  •  轮回少年
    2020-12-09 07:24

    Because I had to make a comparison for a DateTime? and I had make this modification.

    else if (left.Type == typeof(DateTime?) && right.Type == typeof(string))
    {
      if (right is ConstantExpression)
      {
        DateTime datevalue;
        string value = ((ConstantExpression)right).Value.ToString();
        if (DateTime.TryParse(value, out datevalue))
        {
          DateTime? nullableDateValue = datevalue;
          right = Expression.Constant(nullableDateValue, typeof(DateTime?));
        }
        else
        {
          throw IncompatibleOperandsError(op.text, left, right, op.pos);
        }
      }
      else
      {
        throw IncompatibleOperandsError(op.text, left, right, op.pos);
      }
    }
    

    Thanks for the tip!

提交回复
热议问题