首页 新闻 会员 周边

LINQ to Entities 仅支持强制转换 Entity Data Model 基元类型

0
[已关闭问题] 关闭于 2012-09-10 10:06
 /// <summary>
        /// 获取数据列表【此方法存在性能问题】
        /// </summary>
        /// <param name="where"></param>
        /// <param name="top"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public IList<T> GetList(Expression<Func<T, bool>> expWhere, int top, Expression<Func<T, object>> order)
        {
            return _dbSet.Where(expWhere).Take(top).AsQueryable().OrderByDescending(order).ToList();
            //.OrderByDescending(order).AsQueryable().Where(expWhere).Take(top).ToList();
        }

 

public class Repository<T> where T : class
    {
        public readonly DBContext _context;
        public readonly DbSet<T> _dbSet;


        public Repository()
        {
            if (_context == null)
            {
                this._context = new DBContext();
            }
            this._dbSet = this._context.Set<T>();
        }
}

 

        /// <summary>
        /// 获取数据列表【此方法存在性能问题】
        /// </summary>
        /// <param name="where"></param>
        /// <param name="top"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public IList<T> GetList(Expression<Func<T, bool>> expWhere, int top, Expression<Func<T, object>> order)
        {
            return _dbSet.Where(expWhere).Take(top).AsQueryable().OrderByDescending(order).ToList();
            //.OrderByDescending(order).AsQueryable().Where(expWhere).Take(top).ToList();
        }
        private static Repository<NewInfo> ___Dal_NewInfo;
        /// <summary>
        /// NewInfo
        /// </summary>
        protected static Repository<NewInfo> __Dal_NewInfo
        {
            get
            {
                if (___Dal_NewInfo == null)
                {
                    ___Dal_NewInfo = new Repository<NewInfo>();
                }
                return ___Dal_NewInfo;
            }
        }
            vModel.HotNews = __Dal_NewInfo.GetList(n => n.IsOK == 1 && (n.CateID == 16 || n.CateID == 17), 10, n => n.NewID);

 

 

 

linq to entity 排序的时候,OrderBY()接收 ExPression<Func<T,Object> exp>参数,但是我在调用的时候提交 int 数据他提示 LINQ to Entities 仅支持强制转换 Entity Data Model 基元类型  何解?

西安-DB的主页 西安-DB | 初学一级 | 园豆:4
提问于:2012-08-31 18:59
< >
分享
所有回答(3)
0

把你的代码贴出来看看

jerry-Tom | 园豆:4077 (老鸟四级) | 2012-09-03 10:07

你好,代码已经提到问题补充了。麻烦请帮我看下。

另:怎么我收到2封提醒邮件说你的回复,怎么到这里又剩下一个回复了。。

支持(0) 反对(0) 西安-DB | 园豆:4 (初学一级) | 2012-09-03 10:23

@周煜皓: 

 ExPression<Func<T,Object> exp>参数 传的是???把参数也贴出来。光看方法,看不出来。

支持(0) 反对(0) jerry-Tom | 园豆:4077 (老鸟四级) | 2012-09-03 10:54

@百灵: 最后一段代码是所有的参数调用  ExPression<Func<T,Object> exp> 是下边红色部分。

vModel.HotNews = __Dal_NewInfo.GetList(n => n.IsOK == 1 && (n.CateID == 16 || n.CateID == 17), 10, n => n.NewID);
支持(0) 反对(0) 西安-DB | 园豆:4 (初学一级) | 2012-09-05 08:49
0

楼主问题解决勒么?

雷明nice | 园豆:202 (菜鸟二级) | 2012-10-30 11:54
0

。。我也遇到这个问题。解决了么

阿里里 | 园豆:161 (初学一级) | 2013-06-26 17:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册