The Include path expression must refer to a navigation property defined on the type.in eager loading(包含路径表达式必须引用 type.in 预加载中定义的导航属性)
问题描述
我尝试包含这样的匿名类型:除了 CompanyTitle
,PeriodTypeName
)
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 :
但我得到以下异常:
包含路径表达式必须引用导航属性在类型上定义.使用虚线路径进行参考导航属性和用于集合导航的 Select 运算符特性.参数名称:路径
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
结果应该是 Gridview 的数据源.
推荐答案
您不能使用 Include 来选择这样的数据.Include 用于加载相关数据.您应该使用 Include 加载您的实体,然后选择您想要的.请记住从 CompanyId 中删除 .ToString().EF 会为你做这件事.您的查询应如下所示:
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();
这篇关于包含路径表达式必须引用 type.in 预加载中定义的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:包含路径表达式必须引用 type.in 预加载中定义的导航属性


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01