0

我有以下查询:

        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);

如果我取出group,它运行良好,并且(parentCompanies, cProfile, ...)一旦我将group背面放入其中就会包括所有孩子,它会丢失所有孩子。我该如何解决这个问题?

更新

我想我还应该包括我用来放入结果的视图模型。

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 回答