0

One of my datagrid tables is working x100 times slower than the others(takes 15 seconds to load 5 records out of 120 with lazy loading). In my service layer I return the records like this:

users = personRepository.GetAll()
    .GroupBy(g => g.PersonId)
    .Select(a => a.FirstOrDefault())
    .Join(personRepository.GetAll(), 
        m => m.PersonId, 
        m => m.PersonId, 
        (state1, state2) => new { state1, state2})
    .GroupBy(g => g.state2.PersonId)
    .Select(a => a.OrderByDescending(t => t.state2.Date).FirstOrDefault())
    .OrderByDescending(o => o.state2.Id)
    .Select(s => new UsersSG()
        {//***********ATTENTION***********
            state1= s.state1,
            state2= s.state2
        }); 

In the part with "ATTENTION" written, I map the anonymous type's properties to my viewmodel's virtual properties but I'm not sure if this breaks the lazy loading or not. Corresponding viewmodel is defined like this:

public virtual AkrKisiDurum state1 { get; set; }
public virtual AkrKisiDurum state2 { get; set; }

After I return the IQueryable list, I apply filtering, order by and paging to the query just like my other tables and return the result to view. I reviewed the generated query and nothing fancy appears in that.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
yakya
  • 4,559
  • 2
  • 29
  • 31

1 Answers1

1

Yes, it is brakes lazy-loading, because you are creating instance of UsersSG type. Lazy-loading is provided by custom classes derived from your entities (see Working with Proxies). That's why lazy-loadable navigation properties should be virtual - Entity Frameworks provides DbContext to class inherited from entity, and overrides these properties to use DbContext for lazy-loading data.

Also if you assign data to some virtual navigation property, it will not use lazy-loading further, because you already have provided data.

Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459