The Include path expression must refer to a navigation property defined on the type.in eager loading

asp.net c# entity-framework entity-framework-6 linq

Question

I try to include anonymous type like this : I want all incomelist attributes in addition to CompanyTitle ,PeriodTypeName )

 var incomeList = ctx.IncomeLists.Include(i => new
                {
                    CompanyTitle = i.CompanyId.ToString() + "/" + i.Company.CompanyName,
                    PeriodTypeName = i.ListPeriods.Select(lp => lp.PeriodType.PeriodTypeName)
                }).ToList()

My model section like this : enter image description here

but i get the following exception :

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. Parameter name: path

The result should be the datasource to Gridview.

1
15
7/30/2016 5:07:06 PM

Accepted Answer

You cannot use Include to select data like this. Include is used to load related data. You should load your entities using Include then select what you want. Remember to remove .ToString() from CompanyId. EF will do it for you. Your query should look like this:

var incomeList = ctx.IncomeLists
    .Include(i => i.Company)
    .Include(i => i.ListPeriods.Select(lp => lp.PeriodType))
    .Select(i => new 
    {
        CompanyTitle =  i.CompanyId + "/" + i.Company.CompanyName,
        PeriodTypeNames = i.ListPeriods.Select(lp => lp.PeriodType.PeriodTypeName)
    })
    .ToList();
23
9/28/2017 5:04:38 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow