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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...