简体   繁体   中英

Linq Parent/child relationship - no intellisense for child

I have a parent/child table relation ship

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

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)
            };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM