简体   繁体   中英

Parent child relation in Linq query

In the Entity Class

I have two parent table "TestParent" and "TestTag" in Entity class and one of the child table "TestChild" which is not updated in Entity class. so can I Join the TestParent and TestChild table (both are foreinkey primary relation).

Issue: I am not able to select child table TestChild in Linq query

Table : TestTag:

在此处输入图片说明

Table: TestParent:

在此处输入图片说明

Childtable: TestChild

在此处输入图片说明

In Entity class below

public DbSet<TestParent> Questions { get; set; }
  public DbSet<TestTag> Tags { get; set; }

So you have three tables: a table of TestTags , a table of TestParents and a table with TestChildren .

Every TestTag has zero or more TestChildren ; every TestChild belongs to exactly one TestTag , using a foreign key. A straightforware one-to-many relation

Every TestParent also has zero or more TestChildren ; every TestChild has exactly one TestParent , using a foreign key.

If you followed the entity framework code first conventions , you would have something similar to

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

In entity framework the columns of the tables are represented by non-virtual properties. The virtual properties represent the relations between the tables (one-to-many, many-to-many, ...)

This way it is possible for TestTag 4 and TestParent 5 to have two Children. If you don't want, consider creating a combined primary key [TestTagId, TestParentId]

If TestChild has no other properties than the foreign keys, than in fact you are creating a many-to-many relation between TestTag and TestParent. In that case you don't have to mention the TestChild table.

For completeness the DbContext:

class MyDbContext : DbContext
{
     public DbSet<TestTag> TestTags {get; set;}
     public DbSet<TestParent> TestParents {get; set;}
     public DbSet<TestChild> TestChildren {get; set;}
}

This is all that entity framework needs to know to detect your relations and the primary and foreign keys. Because I followed the conventions attributes nor fluent API is necessary, except maybe because of the non-standard plural form Children .

so can I Join the TestParent and TestChild table

Well you can do a (group-)join, but it is usually easier to use the ICollections

Give me the TestParents that ... with all their TestChildren and TestTags that ...

var result = dbContext.TestParents
    .Where(parent => ...)             // if you don't want all testParents
    .Select(parent => new
    {
        // select only the properties you plan to use
        Id = parent.Id,
        Name = parent.Name,

        Children = parent.TestChildren
            .Where(child => child.TestTag.Name = "My Test Tag Name")
            .Select(child => new
            {
                Name = child.TestTag.Name,
                ...
            })
            .ToList(),
    });

Some people prefer to do a GroupJoin instead. If you want that, and you can convince your project leader that a GroupJoin is better readable / testable / maintainable than using the ICollections, you can do the following:

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

IMHO: this looks horrible!

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