Linq Parent/child relationship - no intellisense for child
I have a parent/child table relation ship
ParentTable
ParentID I开发者_开发技巧nt, 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
I have added the above as linq data entites & the relationship is shown in the designer. However, when I try to use the child table columns they are not shown in the IDE intellisense
var x = from y in db.ParentTable
select new
{
y.ParentDescription,
code = y.ChildTable....(Only the lamda functions are shown in intellisense here)
};
The is no intellisense for the child table & if i type in .ChildDescription I get an error "does not contain a definition for 'ChildDescription'"
I'm missing something, presumably.
That is because it is one-to-many relationship and your ChildTable
property is collection of all related children - it is not single child. If you want to select ParentDescription
, ChildDescription
pairs try this:
var x = from y in db.ParentTable
from x in y.ChildTable
select new
{
y.ParentDescription,
code = x.ChildDescription
};
Edit:
You can also do this to get ParentDescription
and collection of all related ChildDescriptions
:
var x = from y in db.ParentTable
select new
{
y.ParentDescription,
codes = y.ChildTable.Select(x => x.ChildDescription)
};
精彩评论