LINQ2SQL表达式-转换日期时转换失败

时间:2018-10-29 13:25:32

标签: c# lambda linq-to-sql

我有这个Linq表达式

var searchDate = DateTime.ParseExact("17.12.2018", "dd.MM.yyyy", 
                                      CultureInfo.InvariantCulture, 
                                      DateTimeStyles.None);

result = result.Where(DatePredicate("LaunchWeekStartDate", searchDate, 3));

我称之为表达式

public static Expression<Func<General, bool>> DatePredicate(string columnName, 
                                                            DateTime 
                                                            searchValue, 
                                                            int? operatorId)
{
  var type = typeof(General);
  var x = Expression.Parameter(type, "x");
  var member = Expression.Property(x, columnName);
  Expression expression;

  var constant = Expression.Constant(searchValue, typeof(DateTime));

  // Greater Than >
  if (operatorId == 2)
  {
    expression = Expression.GreaterThan(member, constant); // THIS THROW ERROR
    return Expression.Lambda<Func<General, bool>>(expression, x);
  }

  // Less Than <
  else if (operatorId == 4)
  {
    expression = Expression.LessThan(member, constant); // THIS THROW ERROR
    return Expression.Lambda<Func<General, bool>>(expression, x);
  }

  // Equal
  var column = type.GetProperties().FirstOrDefault(p => p.Name == columnName);

  expression = column == null
             ? expression = Expression.Constant(true) // THIS WORKS
             : expression = Expression.Equal(member, constant); // THIS THROW ERROR

  return Expression.Lambda<Func<General, bool>>(expression, x);
}

我得到的错误是

  

System.Data.SqlClient.SqlException(0x80131904):从字符串转换日期和/或时间时转换失败。

这有效

var searchDate = DateTime.ParseExact("17.12.2018", "dd.MM.yyyy", 
                                     CultureInfo.InvariantCulture, 
                                     DateTimeStyles.None);
result = result.Where(x => x.LaunchWeekStartDate == searchDate);

LaunchWeekStartDate的数据库列和类属性的类型为DateTime。

public class General
    {
        [Key]
        public Int64 RowNumber {get; set;}
    // ...
        public DateTime LaunchWeekStartDate { get; set; }
    }

请,您能解释一下这里的问题吗?

编辑:

我尝试将Express.EqualExpress.Call方法结合使用,而不是CompareTo

var member = Expression.Property(x, columnName);
MethodInfo method = typeof(DateTime).GetMethod("CompareTo", new[] { typeof(DateTime) });
constant = Expression.Constant(searchValue, typeof(DateTime));
var call = Expression.Call(member, method, constant);
return Expression.Lambda<Func<General, bool>>(call, x);

但是我遇到了一个我不理解的例外:

  

System.ArgumentException:类型'System.Int32'的表达式不能用于返回类型'System.Boolean'

2 个答案:

答案 0 :(得分:2)

看看我在这篇文章中的回答here

解决方案是如何让EntityFramework 生成一个带参数的SqlCommand,以便Data Type Mapping 可以将CLR DateTime 转换为SQL Server 等价物。

答案 1 :(得分:0)

这有效,我从这个已有9年历史的博客https://www.tabsoverspaces.com/231060-comparing-date-only-in-ef

中复制了它
expression = Expression.And(
                Expression.Equal(
                    Expression.MakeMemberAccess(member, typeof(DateTime).GetMember("Day").Single()),
                    Expression.Constant(searchValue.Day)
                    ),
                Expression.And(
                    Expression.Equal(
                        Expression.MakeMemberAccess(member, typeof(DateTime).GetMember("Month").Single()),
                        Expression.Constant(searchValue.Month)
                        ),
                    Expression.Equal(
                        Expression.MakeMemberAccess(member, typeof(DateTime).GetMember("Year").Single()),
                        Expression.Constant(searchValue.Year)
                        )
                    )
                );

但是对于我来说,完成这项任务有点复杂。

我也不知道如何用这种方法制作GreaterThanLessThen:/

编辑:现在,它也适用于大于和小于

// Greater Than >
expression = Expression.GreaterThan(Expression.MakeMemberAccess(member, typeof(DateTime).GetMember("Date").Single()), constant);

// Less Than <
expression = Expression.LessThan(Expression.MakeMemberAccess(member, typeof(DateTime).GetMember("Date").Single()), constant);

// Equal
expression = Expression.Equal(Expression.MakeMemberAccess(member, typeof(DateTime).GetMember("Date").Single()), constant);