简体   繁体   English

使用Entity Framework和LINQ从数据库查询动态类别/子类别

[英]Querying Dynamic Category /Sub Category from database with Entity Framework and LINQ

I need to come up with a solution to have infinite category and subcategories in a database. 我需要提出一个解决方案,以在数据库中具有无限的类别和子类别。 So this is my idea: creating a table that references parents/child as follows: 所以这就是我的想法:创建一个引用父母/孩子的表,如下所示:

在此处输入图片说明

As you can see items can be followed down by its id and ParentItemID . 如您所见,项目后可以跟随其idParentItemID However I don't know how I can apply this solution using LINQ. 但是,我不知道如何使用LINQ应用此解决方案。

I have create a model for above table as follow: 我为上表创建了一个模型,如下所示:

    class Items
    {
        public int id { get; set; }
        public string ItemName { get; set; }
        public int ParentItemID { get; set; }
    }

I have loaded list of 'Items' form database using EF, and I would like to query the list so it can be grouped and browsed like this: 我已经使用EF加载了“ Items”表单数据库的列表,我想查询该列表,以便可以对其进行分组和浏览,如下所示:

在此处输入图片说明

How can it be done? 如何做呢?

Thank in advance for any help. 在此先感谢您的帮助。

1) With your model Add a foreign key (self reference) between parent item id and item id. 1) 使用模型在父项ID和项ID之间添加外键(自引用)。

CREATE TABLE [dbo].[26957446_All]([id] [int] NOT NULL,[name] [varchar](50) NOT NULL, [parentid] [int] NULL, CONSTRAINT [PK_26957446_All] PRIMARY KEY CLUSTERED ( [id] ASC )
ALTER TABLE [dbo].[26957446_All]  WITH CHECK ADD  CONSTRAINT [FK_26957446_Circular] FOREIGN KEY([parentid]) REFERENCES [dbo].[26957446_All] ([id])
ALTER TABLE [dbo].[26957446_All] CHECK CONSTRAINT [FK_26957446_Circular]

Generated model: 生成的模型: 您的模型 , notice the self reference, names auto generated, All1 are children, All2 is parent reference. ,注意自我参考,自动生成的名称, All1是儿童, All2是家长参考。

  var ents = new DynamicCategory.StackOverflowEntities26957446All();
  // all root items loop
  foreach (var item in ents.C26957446_All.Where(x => x.C26957446_All2 == null))
  {
    Console.WriteLine("Id: {0} Name: {1}", item.id, item.name);
    PrintChildrenRecursively(item);
  }  

  static int i = 1; //level
  static void PrintChildrenRecursively (DynamicCategory.C26957446_All item)
  {
    foreach (var c in item.C26957446_All1)
    {
      Console.WriteLine("{2} Child Id: {0} Name: {1}", c.id, c.name, new string('\t', i));

    if (c.C26957446_All1.Count > 0)
    {
      i++;
      PrintChildrenRecursively(c);
      i--;
    }
    Console.WriteLine();
    }
  }

Output: 输出: 先输出

2) With a different db organization . 2) 使用不同的数据库组织 Separate items and relationships into separate tables. 将项目和关系分离到单独的表中。 Add foreign keys from Relationship.Child and Relationship.Parent to Items table. 将Relationship.Child和Relationship.Parent中的外键添加到Items表。

CREATE TABLE [dbo].[26957446_Items]([id] [int] NOT NULL, [name] [varchar](50) NOT NULL, CONSTRAINT [PK_26957446_Items] PRIMARY KEY CLUSTERED ([id] ASC))
CREATE TABLE [dbo].[26957446_Relationships](ParentId] [int] NOT NULL, [ChildId] [int] NOT NULL, CONSTRAINT [PK_Relationships] PRIMARY KEY CLUSTERED ([ParentId] ASC, [ChildId] ASC ))
ALTER TABLE [dbo].[26957446_Relationships]  WITH CHECK ADD  CONSTRAINT [FK_Child] FOREIGN KEY([ChildId]) REFERENCES [dbo].[26957446_Items] ([id]) 
ALTER TABLE [dbo].[26957446_Relationships] CHECK CONSTRAINT [FK_Child] 
ALTER TABLE [dbo].[26957446_Relationships]  WITH CHECK ADD  CONSTRAINT [FK_Parent] FOREIGN KEY([ParentId]) REFERENCES [dbo].[26957446_Items] ([id]) 
ALTER TABLE [dbo].[26957446_Relationships] CHECK CONSTRAINT [FK_Parent]

Generated EF: 生成的EF: 两桌模型 , notice only class generated with both bottom-up and top-down references. ,请注意仅使用自下而上和自上而下的引用生成的类。 Output is identical, code is very much similar with the exception: item.ItemChildren are children, and root items acces is : 输出是相同的,但代码非常相似,不同的是: item.ItemChildren是子代,根项acces是:

var ents = new DynamicCategory.StackOverflowEntities26957446();
ents.C26957446_Items.Where (x=>x.ItemParents.Count==0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM