Linq To EF 用泛型时生成的Sql会查询全表的问题,linqef

1.标题标场地

 public class LinqHepler<T> where T:class
    {
        private EFDBContext _context = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public LinqHepler(EFDBContext context)
        {
            _context = context;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities(Func<T, bool> whereLambda)
        {
            return _context.Set<T>().Where(whereLambda).AsQueryable();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> LoadEntitiesExpression(Expression<Func<T, bool>> whereLambda)
        {
            return _context.Set<T>().Where(whereLambda).AsQueryable();
        }
    }

 

[HttpGet]
        [Route("LinqWhere")]
        public IActionResult LinqWhere()
        {
            string uname = "";
            _context.UserInfo.Where(c => c.UserName == uname).ToList();//带条件
            LinqHepler<UserInfo> linqHepler = new LinqHepler<UserInfo>(_context);
            linqHepler.LoadEntities(c => c.UserName == uname).ToList();//查全表
            linqHepler.LoadEntitiesExpression(c => c.UserName == uname).ToList();//不查全表
            return Ok();
        }

生产的sql

2017-10-11T02:59:22.495010Z 2328 Query SELECT `c`.`UserId`,
`c`.`UserName`, `c`.`UserPwd`
FROM `UserInfo` AS `c`
WHERE `c`.`UserName` = ”
2017-10-11T02:59:22.538041Z 2328 Query SELECT `u`.`UserId`,
`u`.`UserName`, `u`.`UserPwd`
FROM `UserInfo` AS `u`
2017-10-11T02:59:22.551050Z 2328 Query SELECT `c`.`UserId`,
`c`.`UserName`, `c`.`UserPwd`
FROM `UserInfo` AS `c`
WHERE `c`.`UserName` = ”

 

可知,第三个是用EF对象直接调用Linq的强大方法Where能够生成对应的Where条件,

其次个用泛型传递要查学的实业类型和Where条件去查询,那时未有生育对应的Where条件的sql

其四个加了Expession类型包装Func对象有生育Where sql

 

Linq To EF 用泛型时生成的Sql会询问全表的标题,linqef

参照大神的分解

园友问:EF写linq的时候,生成的SQL不带where条件,给全表都查出来的,请问那是如何来头

0
悬赏园豆:50 [已解决难题] 浏览: 575次
本人用的多少存款和储蓄,小编的查询linq如下:
//查询
public IQueryable<T> Query(Func<T, bool> wherelambda)
{
return
db.Set<T>().AsNoTracking<T>().Where<T>(wherelambda).AsQueryable();
}
本身的linq是调用该查询办法,然则通过ef
sql拦截生成的SQL是全表扫描,而通过监测 sql profile生成的sql也是不带SQL

答:宏观的分解是Expression内部是特地有个深入分析器privoder,会依赖规则分析成对应sql并实行到数据库,而Func那一个破委托类型未有这种职业的sql分析器,无法生成你想要的sql,直接是等默许全表加载后在内部存款和储蓄器中筛选。

一.题指标场合

 public class LinqHepler<T> where T:class
    {
        private EFDBContext _context = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public LinqHepler(EFDBContext context)
        {
            _context = context;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities(Func<T, bool> whereLambda)
        {
            return _context.Set<T>().Where(whereLambda).AsQueryable();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> LoadEntitiesExpression(Expression<Func<T, bool>> whereLambda)
        {
            return _context.Set<T>().Where(whereLambda).AsQueryable();
        }
    }

 

[HttpGet]
        [Route("LinqWhere")]
        public IActionResult LinqWhere()
        {
            string uname = "";
            _context.UserInfo.Where(c => c.UserName == uname).ToList();//带条件
            LinqHepler<UserInfo> linqHepler = new LinqHepler<UserInfo>(_context);
            linqHepler.LoadEntities(c => c.UserName == uname).ToList();//查全表
            linqHepler.LoadEntitiesExpression(c => c.UserName == uname).ToList();//不查全表
            return Ok();
        }

生产的sql

2017-10-11T02:59:22.495010Z 2328 Query SELECT `c`.`UserId`,
`c`.`UserName`, `c`.`UserPwd`
FROM `UserInfo` AS `c`
WHERE `c`.`UserName` = ”
2017-10-11T02:59:22.538041Z 2328 Query SELECT `u`.`UserId`,
`u`.`UserName`, `u`.`UserPwd`
FROM `UserInfo` AS `u`
2017-10-11T02:59:22.551050Z 2328 Query SELECT `c`.`UserId`,
`c`.`UserName`, `c`.`UserPwd`
FROM `UserInfo` AS `c`
WHERE `c`.`UserName` = ”

 

足见,第3个是用EF对象直接调用Linq的扩张方法Where能够更改对应的Where条件,

其次个用泛型传递要查学的实业类型和Where条件去查询,那时未有生育对应的Where条件的sql

其四个加了Expession类型包装Func对象有生产Where sql

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注