问题描述
|
我有一个父母/子女表关系船
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)
};