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

后端 未结 1 1882
眼角桃花
眼角桃花 2020-12-11 01:06

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

<         


        
相关标签:
1条回答
  • 2020-12-11 01:44

    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();
    
    0 讨论(0)
提交回复
热议问题