首页 新闻 会员 周边 捐助

linq动态表达式问题

0
[待解决问题]

用帮助类生成动态表达式使用linq语句查询,但是生成的sql语句报错

1、帮助类:

   public static Expression<Func<T, bool>> True<T>() { return param => true; }
        public static Expression<Func<T, bool>> False<T>() { return param => false; }
        /// <summary>
        /// 组合And
        /// </summary>
        /// <returns></returns>
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.AndAlso);
        }
        /// <summary>
        /// 组合Or
        /// </summary>
        /// <returns></returns>
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.OrElse);
        }

        /// <summary>
        /// Combines the first expression with the second using the specified merge function.
        /// </summary>
        static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
        {
            var map = first.Parameters
                .Select((f, i) => new { f, s = second.Parameters[i] })
                .ToDictionary(p => p.s, p => p.f);
            var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
            return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
        }

2、声明:

var expression = LinqExtensions.True<Logis_shouhuoHeader>();
var expression2 = LinqExtensions.True<Logis_fahuoHeader>();

3、表达式赋值:

 if (!StringExtensions.IsEmpty(queryParam["fahuorenName"].ToString()))
 {
     string keyword = queryParam["fahuorenName"].ToString();
     expression = expression.And(t => t.fahuorenName.Contains(keyword));
 }
 if (!StringExtensions.IsEmpty(queryParam["liushuihao"].ToString()))
 {
     string keyword = queryParam["liushuihao"].ToString();
     expression = expression.And(t => t.liushuihao.Contains(keyword));
 }

4、使用:

 tempall = db.Queryable<Logis_shouhuoHeader>()
     .JoinTable<Logis_fahuoDetail>((s1, s2) => s1.ID == s2.shouhuoHeaderID, JoinType.INNER)
     .JoinTable<Logis_fahuoDetail, Logis_fahuoHeader>((s1, s2, s3) => s2.HeaderID == s3.ID, JoinType.INNER).Where(expression)

5、生成:

EXEC sp_executesql N'SELECT COUNT(1)  FROM [Logis_shouhuoHeader]    s1 INNER JOIN [Logis_fahuoDetail]  s2 ON  ( s1.ID  = s2.shouhuoHeaderID )    INNER JOIN [Logis_fahuoHeader]  s3 ON  ( s2.HeaderID  = s3.ID )   WHERE 1=1  AND ( ( (1=1)  AND   ([param].[DateTihuo] >= @param_DateTihuo301)  )  AND   ([param].[DateTihuo] <= @param_DateTihuo302)  )   ',
    N'@param_DateTihuo301 datetime,@param_DateTihuo302 datetime',
    @param_DateTihuo301 = '2017-05-02 00:00:00',
    @param_DateTihuo302 = '2017-05-03 23:59:59';

6、最后:sql错误:动态生成的是[param]并没有在前面做生成标记,所以执行sql报错

消息 4104,级别 16,状态 1,第 1 行
无法绑定由多个部分组成的标识符 "param.DateTihuo"。
消息 4104,级别 16,状态 1,第 1 行
无法绑定由多个部分组成的标识符 "param.DateTihuo"。

这样的错误如何处理???????是哪里出了问题????

木头园—OOIP的主页 木头园—OOIP | 菜鸟二级 | 园豆:212
提问于:2017-05-03 11:22
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册