首页 新闻 会员 周边

LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke”

0
悬赏园豆:20 [已关闭问题] 关闭于 2014-03-26 20:19
 1  public virtual ListViewResponseResult<TV> GetEntitys<TS>( int pageIndex, int pageSize, int fId, Expression<Func<TD, bool>> whereLambda, Expression<Func<TD, TS>> orderByLambda, bool isAsc)
 2 {
 3    .......
 4 
 5        var temp = Repository.GetEntitysByFId(fId);
 6             if (whereLambda != null)
 7             {
 8                 temp = temp.Where(whereLambda);
 9             }        
10    ........
11 }

通过EF查询 ,这是一个分页的公共方法,其中whereLambda是用于查询的where条件,我想在外部通过表达式树进行组装where查询条件,调用代码如下

internal static class PredicateExtensions
    {
        public static Expression<Func<T, bool>> True<T>() { return f => true; }

        public static Expression<Func<T, bool>> False<T>() { return f => false; }

        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1,
           Expression<Func<T, bool>> expression2)
        {
            var invokedExpression = Expression.Invoke(expression2, expression1.Parameters);

            return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression),
            expression1.Parameters);
        }

        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,
              Expression<Func<T, bool>> expression2)
        {
            var invokedExpression = Expression.Invoke(expression2, expression1.Parameters);

            return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body,
                   invokedExpression), expression1.Parameters);
        }
}

  
//组装whereLambda  scanType,startTime,endTime都是参数
            Expression<Func<ChannelScanStatistics, bool>> where = PredicateExtensions.True<ChannelScanStatistics>();
            if (scanType.HasValue)
            {
                short type = (short)scanType.Value;
                where = where.And(e => e.ScanType == type);
            }
            if (startTime.HasValue)
            {
                where = where.And(e => e.CreateTime >= startTime);
            }
            if (endTime.HasValue)
            {
                DateTime end = endTime.Value.Date.AddDays(1);
                where = where.And(e => e.CreateTime < end);
            }

调用的时候就会报错:

LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke”。

 

试着将Expression<Func<TD, bool>> whereLambda改为Func<TD, bool>>whereLambda,发现生成的sql语句实际没有生成where语句,而是整个表查询,请问如何来实现表达式树的linq to entity

sjR10的主页 sjR10 | 初学一级 | 园豆:169
提问于:2014-03-26 20:01
< >
分享
所有回答(2)
0

已解决

http://blog.csdn.net/leewhoee/article/details/8968023

sjR10 | 园豆:169 (初学一级) | 2014-03-26 20:13
0

兄弟,你怎么解决的?

你猜丶 | 园豆:183 (初学一级) | 2018-11-08 11:19

EF只支持他能转换的linq脚本 我这个里面有个判断是
if (startTime.HasValue)
{
where = where.And(e => e.CreateTime >= startTime);
}
而EF不支持可控类型的DateTime
需要这样改
if (startTime.HasValue)
{
var timeTemp = startTime.Value;
where = where.And(e => e.CreateTime >= timeTemp );
}
同理 下面的endTime一样需要处理下
你的lamada表达式看下 是不是也有类似的不可以转换的类型 细心观察下

支持(0) 反对(0) sjR10 | 园豆:169 (初学一级) | 2018-11-11 19:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册