C# Linq查询中的父子关系
在实体类中 我在实体类中有两个父表“TestParent”和“TestTag”,还有一个子表 实体类中未更新的“TestChild”。因此,我可以加入TestParent和TestChild表(两者都是foreinkey主关系) 问题:我无法在Linq查询中选择子表TestChild 表:TestTag: 表:TestParent: Childtable:TestChild 在下面的实体类中C# Linq查询中的父子关系,c#,entity-framework,linq,C#,Entity Framework,Linq,在实体类中 我在实体类中有两个父表“TestParent”和“TestTag”,还有一个子表 实体类中未更新的“TestChild”。因此,我可以加入TestParent和TestChild表(两者都是foreinkey主关系) 问题:我无法在Linq查询中选择子表TestChild 表:TestTag: 表:TestParent: Childtable:TestChild 在下面的实体类中 public DbSet<TestParent> Questions { get; s
public DbSet<TestParent> Questions { get; set; }
public DbSet<TestTag> Tags { get; set; }
公共数据库集问题{get;set;}
公共DbSet标记{get;set;}
因此您有三个表:一个包含TestTags
的表,一个包含TestParents
的表和一个包含TestChildren
的表
每个TestTag
都有零个或多个TestChildren
;使用外键,每个TestChild
恰好属于一个TestTag
。一对多关系
每个TestParent
也有零个或多个TestChildren
;使用外键,每个TestChild
都只有一个TestParent
如果你遵循,你会有类似的东西
class TestTag
{
public int Id {get; set;}
... // other properties
// every TestTag has zero or more TestChildren (one-to-many)
public virtual ICollection<TestChild> TestChildren {get; set;}
}
class TestParent
{
public int Id {get; set;}
... // other properties
// every TestParent has zero or more TestChildren (one-to-many)
public virtual ICollection<TestChild> TestChildren {get; set;}
}
class TestChild
{
public int Id {get; set;}
... // other properties
// every TestChild has exactly one TestParent using foreign key
public int TestParentId {get; set;}
public virtual TestParent TestParent {get; set;}
// every TestChild has exactly one TestTag using foreign key
public int TestTagId {get; set;}
public virtual TestTag TestTag {get; set;}
}
有些人更喜欢加入群组。如果您希望这样做,并且您可以说服您的项目负责人,GroupJoin比使用ICollections更具可读性/可测试性/可维护性,那么您可以执行以下操作:
var result = dbContext.TestParents.GroupJoin(dbContext.TestChildren,
parent => parent.Id, // from each parent take the Id
child => child.ParentId, // from each child take the ParentId
// resultSelector:
(parent, children) => new
{
// Parent Properties:
Id = parent.Id,
...
// GroupJoin the collection of child properties with the TestTags:
children.GroupJoin(dbContext.TestTags,
child => child.TestTagId, // from each TestChild take the TestTagId
tag => tag.Id, // from each TestTag take the Id
(child, tagsOfThisChild) => new
{
// desired Child properties
...
TestTags = tagsOfThisChild.Select(tag => new
{
Id = tag.Id,
...
})
.ToList(),
})
.ToList(),
});
这看起来很可怕 因此您有三个表:一个包含
TestTags
的表,一个包含TestParents
的表和一个包含TestChildren
的表
每个TestTag
都有零个或多个TestChildren
;使用外键,每个TestChild
恰好属于一个TestTag
。一对多关系
每个TestParent
也有零个或多个TestChildren
;使用外键,每个TestChild
都只有一个TestParent
如果你遵循,你会有类似的东西
class TestTag
{
public int Id {get; set;}
... // other properties
// every TestTag has zero or more TestChildren (one-to-many)
public virtual ICollection<TestChild> TestChildren {get; set;}
}
class TestParent
{
public int Id {get; set;}
... // other properties
// every TestParent has zero or more TestChildren (one-to-many)
public virtual ICollection<TestChild> TestChildren {get; set;}
}
class TestChild
{
public int Id {get; set;}
... // other properties
// every TestChild has exactly one TestParent using foreign key
public int TestParentId {get; set;}
public virtual TestParent TestParent {get; set;}
// every TestChild has exactly one TestTag using foreign key
public int TestTagId {get; set;}
public virtual TestTag TestTag {get; set;}
}
有些人更喜欢加入群组。如果您希望这样做,并且您可以说服您的项目负责人,GroupJoin比使用ICollections更具可读性/可测试性/可维护性,那么您可以执行以下操作:
var result = dbContext.TestParents.GroupJoin(dbContext.TestChildren,
parent => parent.Id, // from each parent take the Id
child => child.ParentId, // from each child take the ParentId
// resultSelector:
(parent, children) => new
{
// Parent Properties:
Id = parent.Id,
...
// GroupJoin the collection of child properties with the TestTags:
children.GroupJoin(dbContext.TestTags,
child => child.TestTagId, // from each TestChild take the TestTagId
tag => tag.Id, // from each TestTag take the Id
(child, tagsOfThisChild) => new
{
// desired Child properties
...
TestTags = tagsOfThisChild.Select(tag => new
{
Id = tag.Id,
...
})
.ToList(),
})
.ToList(),
});
这看起来很可怕 是否与TestParent相关?如果没有,请提供
TestParent
实体类?另外,我没有看到TestParent
和TestChild
@BijayKoirala之间的任何关系,很抱歉我已经更新了,请再检查一件事,你是否忘记了更新TestChild
,因为我没有看到TestTag的方式,TestParent和TestChild
是相关的?请假设在TestChild表中->TagId是TestParent表的主键。我的意思是TestParent和TestChild表给出了外键prime-key关系。TestTag表没有任何关系。因此,您无法从Linq
实现连接。如果您不打算修改实体类以添加TestChild
,那么您可以创建存储过程
,在该过程中可以进行连接,并从调用sp
的代码中获取数据。与TestParent
相关吗?如果没有,请提供TestParent
实体类?另外,我没有看到TestParent
和TestChild
@BijayKoirala之间的任何关系,很抱歉我已经更新了,请再检查一件事,你是否忘记了更新TestChild
,因为我没有看到TestTag的方式,TestParent和TestChild
是相关的?请假设在TestChild表中->TagId是TestParent表的主键。我的意思是TestParent和TestChild表给出了外键prime-key关系。TestTag表没有任何关系。因此,您无法从Linq
实现连接。如果您不打算修改实体类来添加TestChild
,那么您可以创建存储过程
,在这里可以执行连接并从调用sp
的代码中获取数据。我只需要基于“TestChild”表TagId返回“TestParents”表记录。我将传递“tagId”,我只需要基于“TestChild”表tagId返回“TestParents”表记录。我将通过“tagId”