0

I have the following query:

        var _customers = (from c in _db.UserProfiles.Include(x=>x.ParentCompanies).Include(x=>x.cProfile).Include(x=>x.cProfile.PhoneNumbers).Include(x=>x.cProfile.Addresses)
                          where (c.ParentCompanies.Any(pc => pc.CompanyUsers.Any(cu => cu.UserName == userName)) && c.cProfile != null)
                          group c by c.FirstName.Substring(0, 1).ToUpper() into customerGroup
                          select new ContactsViewModel
                          {
                              FirstLetter = customerGroup.Key,
                              Customers = customerGroup
                          }).OrderBy(letter => letter.FirstLetter);

if I take out the group, it works well and includes all the children (parentCompanies, cProfile, ...) as soon as I put the group back in it looses all of the children. How do I solve this issue?

update

I guess I should also include the view model that I'm usign to put the result in.

public class ContactsViewModel
{
    public string FirstLetter { get; set; }
    public IEnumerable<UserProfile> Customers { get; set; }
}
4

1 に答える 1

1

Include only applies to items in the query results (i.e. the final projection) and cannot contain operations that change the type of the result between Include and the outermost operation (e.g. GroupBy())

http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework

If you want to eager load, do the grouping client-side (i.e. enumerate the query then call the GroupBy method on the results)

于 2013-03-26T21:23:20.747 に答える