在子记录上应用过滤器

时间:2016-06-02 11:51:44

标签: c# entity-framework automapper

ParentWrapperTable
 ID Name
 1 ppp

ParentTable
ID ParentWrapperID ChildWrapperID ExpiryDate
1 1 1 10/05/2016
2 1 1 NULL

ChildWrapperTable
ID Name
1 ccc

ChildTable
ID ChildWrapperID GrandChildWrapperID ExpiryDate
1 1 1 11/05/2016
2 1 1 NULL

GrandChildWrapperTable
ID Name
1 ggg

GrandChildTable
ID GrandChildWrapperID ExpiryDate
1 1 12/05/2016
2 1 NULL

以上持续多个级别(即grandgrandchild等)。 WrapperTable(即ParentWrapperTable)与普通表(即ParentTable)之间的1-many关系。

我正在使用实体框架。我想获得名称' ppp'的父记录。和expirydate是null并且所有链接的依赖项记录到期日期为null但我只能在父级别上应用过滤器。因此,我获得了一个父记录,2个孩子和2个孙子记录,但它应该是父母,子女和孙子的1条记录。任何人都可以帮助我以通用方式在所有级别(可能超过10个级别)中应用expirydate过滤器吗?

    var series = ctx.ParentTable.Where(s => s.ExpiryDate == null)


            if (series != null && series.Any())
            {
                if (!string.IsNullOrWhiteSpace(name))
                {
                    var wrapper = ctx.ParentWrapperTable.SingleOrDefault(c => c.Name == name);
                    if (wrapper != null)
                    {
                        series = series.Where(c => c.ParentWrapperID == wrapper.ID);

                        if (series.Any())
                            seriesList = series.ToList();
                    }
                }
    }

1 个答案:

答案 0 :(得分:0)

我相信您可以通过Table-Per-Hierarchy实现目标:

我已经重新配置了您的数据库,如下图所示:

Parent Child Grandchild Table

Parent Child Grandchild Wrapper

首先使用代码,我使用以下代码创建了数据库和数据:

static void CreateAndSeedDatabase()
        {
            Context context = new Context();
            ParentWrapper parentWrapper = new ParentWrapper() { Name = "ppp" };
            ChildWrapper childWrapper = new ChildWrapper() { Name = "ccc" };
            GrandChildWrapper grandChildWrapper = new GrandChildWrapper() { Name = "ggg" };

            ParentTable parentTable1 = new ParentTable { ChildWrapper = childWrapper, Wrapper = parentWrapper, ExpiryDate = "10/5/2015" };
            ParentTable parentTable2 = new ParentTable { ChildWrapper = childWrapper, Wrapper = parentWrapper, ExpiryDate = null };

            ChildTable childTable1 = new ChildTable { Wrapper = childWrapper, ChildWrapper = grandChildWrapper, ExpiryDate = "11/5/2015" };
            ChildTable childTable2 = new ChildTable { Wrapper = childWrapper, ChildWrapper = grandChildWrapper, ExpiryDate = null };

            GrandChildTable grandChildTable1 = new GrandChildTable { Wrapper = grandChildWrapper, ExpiryDate = "12/5/2015" };
            GrandChildTable grandChildTable2 = new GrandChildTable { Wrapper = grandChildWrapper, ExpiryDate = null };

            context.Wrappers.Add(parentWrapper);
            context.Wrappers.Add(childWrapper);
            context.Wrappers.Add(grandChildWrapper);

            context.Tables.Add(parentTable1);
            context.Tables.Add(parentTable2);

            context.Tables.Add(childTable1);
            context.Tables.Add(childTable2);

            context.Tables.Add(grandChildTable1);
            context.Tables.Add(grandChildTable2);

            context.SaveChanges();
        }

我的上下文类定义为:

public class Context : DbContext
{
    public Context()
    {
        Database.SetInitializer<Context>(new CreateDatabaseIfNotExists<Context>());
        Database.Initialize(true);
    }

    public DbSet<Wrapper> Wrappers { get; set; }
    public DbSet<Table> Tables { get; set; }
}

我的实体是:

public abstract class Wrapper {
    public int WrapperId { get; set; }
    public string Name { get; set; }
}

public class ParentWrapper: Wrapper{}
public class ChildWrapper:ParentWrapper{}
public class GrandChildWrapper : ChildWrapper { }

public abstract class Table
{
    public int TableId { get; set; }
    public string ExpiryDate { get; set; }
    public Wrapper Wrapper { get; set; }
    public Wrapper ChildWrapper { get; set; }
}

public class ParentTable:Table{}

public class ChildTable:Table{}

public class GrandChildTable:Table{}

以下代码运行您的查询:

    static void QueryDatabase()
    {
        Context context = new Context();
        ParentTable[] result = context.Tables.Include("Wrapper").Include("ChildWrapper").OfType<ParentTable>().Where(t => t.ExpiryDate == null).ToArray();
    }