Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq查询中的父子关系_C#_Entity Framework_Linq - Fatal编程技术网

C# Linq查询中的父子关系

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

在实体类中

我在实体类中有两个父表“TestParent”和“TestTag”,还有一个子表 实体类中未更新的“TestChild”。因此,我可以加入TestParent和TestChild表(两者都是foreinkey主关系)

问题:我无法在Linq查询中选择子表TestChild

表:TestTag:

表:TestParent:

Childtable:TestChild

在下面的实体类中

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”