父/子表未在 LINQ 中引用,还是我错了?

发布于 2024-09-08 05:05:37 字数 783 浏览 0 评论 0原文

我有一个类别表“类别”[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缺⑴份安定 2024-09-15 05:05:37

好吧,看来我应该做的是使用 LINQ 进行相当基本的联接,而不是依赖于上下文类中的联接对象。像这样的东西:

var props = from i in context.Prices
    join e in context.PricingEntities on i.EntityID equals e.EntityID
    join l in context.PricingEntityProperties on e.EntityID equals l.EntityID
    join p in context.PricingProperties on l.PropertyID equals p.PropertyID
    where i.InstanceID == instanceId
    select p;

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:

var props = from i in context.Prices
    join e in context.PricingEntities on i.EntityID equals e.EntityID
    join l in context.PricingEntityProperties on e.EntityID equals l.EntityID
    join p in context.PricingProperties on l.PropertyID equals p.PropertyID
    where i.InstanceID == instanceId
    select p;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文