如何解决Linq亲子关系-对孩子没有智力
| 我有一个父母/子女表关系船ParentTable
ParentID Int,Identity
ParentDescription VarChar
ChildTable
ParentID Int
ChildCode VarChar
ChildDescription VarChar
ParentTable.ParentID is a PK
ChildTable.ParentID is a FK to ParentTable.ParentID
ChildTable.ParentID + ChildTable.ChildCode is a PK
我已将以上内容添加为linq数据实体,并且该关系已在设计器中显示。但是,当我尝试使用子表列时,它们不会在IDE智能感知中显示
var x = from y in db.ParentTable
select new
{
y.ParentDescription,code = y.ChildTable....(Only the lamda functions are shown in intellisense here)
};
子表没有智能感知,如果我输入.ChildDescription,则会收到错误消息“不包含\'ChildDescription \'的定义”
我想念一些东西。
解决方法
那是因为这是一对多的关系,而您的2财产是所有相关子女的集合-它不是单身子女。如果要选择
ParentDescription
,ChildDescription
对,请尝试以下操作:
var x = from y in db.ParentTable
from x in y.ChildTable
select new
{
y.ParentDescription,code = x.ChildDescription
};
编辑:
您也可以这样做以获得to3ѭ和所有相关ChildDescriptions
的集合:
var x = from y in db.ParentTable
select new
{
y.ParentDescription,codes = y.ChildTable.Select(x => x.ChildDescription)
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。