Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在EF中使用Take扩展方法的内存中有多少对象?_C#_Entity Framework_C# 4.0_Entity Framework 4 - Fatal编程技术网

C# 在EF中使用Take扩展方法的内存中有多少对象?

C# 在EF中使用Take扩展方法的内存中有多少对象?,c#,entity-framework,c#-4.0,entity-framework-4,C#,Entity Framework,C# 4.0,Entity Framework 4,我使用的GenericRepository具有Get方法: 我想知道以下代码块之间是否存在加载差异: Expression<Func<PressRelease_ar, bool>> exp = p => p.Id <=5 ; lst = Global.uow.PressReleaseRepository_ar .Get ( filter : exp, orderBy: n => n.Orde

我使用的GenericRepository具有Get方法:

我想知道以下代码块之间是否存在加载差异:

Expression<Func<PressRelease_ar, bool>> exp = p => p.Id <=5 ; 
lst = Global.uow.PressReleaseRepository_ar
      .Get
      (
         filter : exp, 
         orderBy: n => n.OrderByDescending(d => d.Id)
       ).ToList();
有关更多详细信息,请参见Get方法:

    public virtual IEnumerable<TEntity> Get(
                                 Expression<Func<TEntity, bool>> filter = null,
                                 Func<IQueryable<TEntity>,
                                 IOrderedQueryable<TEntity>> orderBy = null,
                                 string includeProperties = "")
   {

      IQueryable<TEntity> query = dbSet;

      if (filter != null) query = query.Where(filter);

      foreach (var includeProperty in includeProperties.Split
                 (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
      {
          query = query.Include(includeProperty);
      }


      if (orderBy != null) return orderBy(query).ToList();
      else return query.ToList();
    }
公共虚拟IEnumerable Get(
表达式筛选器=空,
Func orderBy=null,
字符串includeProperties=“”)
{
IQueryable query=dbSet;
if(filter!=null)query=query.Where(filter);
foreach(includeProperty.Split中的var includeProperty
(新字符[]{',},StringSplitOptions.RemoveEmptyEntries)
{
query=query.Include(includeProperty);
}
if(orderBy!=null)返回orderBy(query).ToList();
else返回query.ToList();
}

您的
Get
方法导致执行一个查询(您正在使用方法末尾的
ToList()
)。这导致了很大的不同:


  • 如果Get-method中有语法错误,第一个查询将查找id为的所有行。您的问题标题令人困惑。即时加载和延迟加载是指加载实体导航属性的不同方法。您的问题与加载导航属性无关,是吗?@Slauma:请给出一个标题。感谢您的回答,满意。“如何在EF中使用Take extension方法加载数据?”或“为什么在EF中使用Take extension方法如此缓慢?”或类似问题,取决于您提出此问题的原因。
        public virtual IEnumerable<TEntity> Get(
                                     Expression<Func<TEntity, bool>> filter = null,
                                     Func<IQueryable<TEntity>,
                                     IOrderedQueryable<TEntity>> orderBy = null,
                                     string includeProperties = "")
       {
    
          IQueryable<TEntity> query = dbSet;
    
          if (filter != null) query = query.Where(filter);
    
          foreach (var includeProperty in includeProperties.Split
                     (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
          {
              query = query.Include(includeProperty);
          }
    
    
          if (orderBy != null) return orderBy(query).ToList();
          else return query.ToList();
        }