父/子表未在 LINQ 中引用,还是我错了?
我有一个类别表“类别”[ID,标题]和一个父/子表“CategoryParent”[ID,CategoryID,ParentCategoryID],其中多对多树由使用 ParentCategoryID 指示的类别的父级表示连接表中的字段。
我认为这将是一个简单的结构,用于检索根类别(在连接表中具有 NULL 作为 ParentCategoryID 值的类别条目)和子类别(按父 id)。
但是,我没有尝试编写 LINQ2SQL 语句来通过连接表中的父 ID 获取类别对象的列表,已生成任何可编译的内容。
我会发布一些代码,但从任何角度来看,它们都不是(a)完整的或(b)合理的。
应该如何解决这个问题呢?
我的连接表如下所示:
CategoryParent
---
ParentCategoryID [int] (PK)
CategoryID [int] FK
CategoryParentID [int] FK
我的数据(类别)表如下所示:
Category
---
CategoryID [int] PK
Title [nvarchar]
有两种关系:
Category.CategoryID 1->* CategoryParent.CategoryID
Category.CategoryID 1->* CategoryParent.ParentCategoryID
我想提供 NULL 或 CategoryID 并获取以其作为父级的所有类别表行。
I have a categories table "Category" [ID, Title] and a Parent/Child table "CategoryParent" [ID,CategoryID,ParentCategoryID] where a many-to-many tree is represented by the parent of a category being indicated using the ParentCategoryID field in the join table.
I thought this would be a simple structure to use to retrieve root categories (category entries which have NULL as a ParentCategoryID value in the join table) and child categories (by parent id.)
However, none of my attempts to write a LINQ2SQL statement to get a list of Category objects by their parent ID in the join table have produced anything compilable.
I would post some code, but none of it is either (a)complete or (b)sensible - in any terms at all.
How should one go about this?
My join table looks like this:
CategoryParent
---
ParentCategoryID [int] (PK)
CategoryID [int] FK
CategoryParentID [int] FK
My data (category) table looks like this:
Category
---
CategoryID [int] PK
Title [nvarchar]
There are two relationships:
Category.CategoryID 1->* CategoryParent.CategoryID
Category.CategoryID 1->* CategoryParent.ParentCategoryID
I would like to provide either NULL or a CategoryID and get back all the Category table rows which have it as a parent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,看来我应该做的是使用 LINQ 进行相当基本的联接,而不是依赖于上下文类中的联接对象。像这样的东西:
Ok, well, it looks like what I should have been doing is a fairly basic join using LINQ, rather than relying on the join objects in the context class. Something like: