Entity Framework: Navigation Properties Issue

前端 未结 4 1195
野的像风
野的像风 2020-12-09 22:05

I am working with Entity Framework code-first, and I have a class Course which has a navigation property Students:

public virtual C         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-09 22:50

    If you were to use proper variable typing then you would see what is happening. The entire set is lazy loaded loaded into memory by the navigation property.

    //user is an instance of the class User referenced by DbSet
    //when you lazy load a navigation property in that set, it loads the data
    ICollection allStudents = user.Students;
    
    //At this point, all of the data was lazy loaded
    //But the Where creates an IEnumerable of the in memory set
    IEnumerable activeStudents = allStudents.Where(n => n.Active);
    
    //At this point, the IEnumerable is iterated, and a List is returned
    List listOfActiveStudents = activeStudents.ToList();
    

提交回复
热议问题