22

I have the following Repository method:-

public AccountDefinition GetCustomer2(int id)
{
    var c = entities.AccountDefinitions
            .Where(p=>p.ORG_ID==id)
            .Include(a => a.SDOrganization)
            .Include(a2 => a2.SiteDefinitions)
            .Include(a3 => a3.SDOrganization.AaaPostalAddresses)
            .Include(a4 => a4.SiteDefinitions.SelectMany
                              (a5 => a5.DepartmentDefinitions.SelectMany
                                    (a6 => a6.SDUsers.Select
                                          (a7 => a7.AaaUser))))
                                                   .SingleOrDefault();

    return c;
}

The the following action method which calls the above method:-

public ActionResult Details2(int id = 0)
{
    AccountDefinition cd = repository.GetCustomer2(id);
    return View("copy",cd);
}

but when i navigate to the Action Method , i get the following error on the repository class:-

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

So what is wrong with my code?

1 Answer 1

29

I think you may want to do something like

public AccountDefinition GetCustomer2(int id)
        {

            var c = entities.AccountDefinitions.Where(p=>p.ORG_ID==id)
                .Include(a => a.SDOrganization)
                .Include(a2 => a2.SiteDefinitions)
                .Include(a3 => a3.SDOrganization.AaaPostalAddresses)
                .Include(a4 => a4.SiteDefinitions.Select(a5 => a5.DepartmentDefinitions.Select(a6 => a6.SDUsers.Select(a7 => a7.AaaUser))));

            return c;
        }
4
  • thanks for the reply, but the SDOrganization does not have a .Select. And if i write your code i will get the following error:-""Error 2 'TMS.Models.SDOrganization' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'TMS.Models.SDOrganization' could be found (are you missing a using directive or an assembly reference?""
    – John John
    Jul 11, 2013 at 15:37
  • 7
    I see, then the problem is probably SelectMany, change SelectMany to Select instead.
    – J.W.
    Jul 11, 2013 at 15:43
  • 1
    i was stuck on some other problem but ur answer pointed me in right direction Jan 23, 2016 at 12:37
  • @AtulChaudhary Me too, but this solution helped. I wanted to include a virtual collection of properties in a root property. This is my resulting query: return this.Entities.Where(x => x.Id == id) .Include(x => x.QuestionSets).AsNoTracking() .Include(x => x.ProductTradeTypeMAPs).AsNoTracking() .Include(x => x.ProductTradeTypeMAPs.Select(y => y.TradeType)).AsNoTracking() .SingleOrDefault();
    – Kevin Dark
    Aug 31, 2016 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.