LINQ查询,连接表问题

发布于 2024-10-17 19:21:05 字数 2064 浏览 1 评论 0原文

我想编写一个查询,为每个事件 (tbl_events) 显示与其相关的所有对象 (tbl_objects)(关系类型 - M:N)。

我对作为连接表(关联类)的表有问题,该表仅保存连接的 2 个表的外键。

例如,tbl_events 通过名为 tbl_object_has_tbl_events 的连接表连接到 tbl_objects

这是连接表的结构:

tbl events has: eventID, eventName

tbl_object has: objectID, objectName

tbl_object_has_tbl_events: eventID, objectID

这是我尝试编写的内容:

IList dataList = (from dEvent in App.glidusContext.tbl_events.
                    join dObject in App.glidusContext.tbl_objects
                    on dEvent.tbl_objects equals dObject.objectID
                    select new { dEvent.eventName, dObject.objectName}).ToList();

我无法访问连接表tbl_object_has_tbl_events

当我具有 M:N 关系时,如何实现此类查询?

更新多对多关系的生成:

-- -----------------------------------------------------
-- Table tbl_events
-- -----------------------------------------------------
CREATE TABLE tbl_events (
    eventID INT NOT NULL IDENTITY,
    eventName NVARCHAR(100) NOT NULL,
    PRIMARY KEY (eventID));

-- -----------------------------------------------------
-- Table tbl_objects
-- -----------------------------------------------------
CREATE TABLE tbl_objects (
    objectID INT NOT NULL IDENTITY,
    objectName NVARCHAR(100) NOT NULL,
    PRIMARY KEY (objectID));

-- -----------------------------------------------------
-- Table tbl_objects_has_tbl_events
-- -----------------------------------------------------
CREATE TABLE tbl_objects_has_tbl_events (
    objectID INT NOT NULL,
    eventID INT NOT NULL,
    PRIMARY KEY (objectID, eventID),
    CONSTRAINT fk_tbl_objects_has_tbl_events_tbl_objects
        FOREIGN KEY (objectID)
            REFERENCES tbl_objects (objectID)
                ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT fk_tbl_objects_has_tbl_events_tbl_events
        FOREIGN KEY (eventID)
            REFERENCES tbl_events (eventID)
                ON DELETE CASCADE ON UPDATE CASCADE);

I want to write a query that presents for every event (tbl_events) all objects (tbl_objects) related to it (relation type — M:N).

I have a problem with tables that are connection tables (association class) that holds only foreign keys of the 2 tables that connects.

For instance, tbl_events is connected with a connection table named tbl_object_has_tbl_events to tbl_objects.

Here is a structure of connected tables:

tbl events has: eventID, eventName

tbl_object has: objectID, objectName

tbl_object_has_tbl_events: eventID, objectID

Here is what I tried to write:

IList dataList = (from dEvent in App.glidusContext.tbl_events.
                    join dObject in App.glidusContext.tbl_objects
                    on dEvent.tbl_objects equals dObject.objectID
                    select new { dEvent.eventName, dObject.objectName}).ToList();

I can't reach the connection table tbl_object_has_tbl_events

How I can implement such query, when I have an M:N relationship?

UPDATE Generation of Many-to-many relationship:

-- -----------------------------------------------------
-- Table tbl_events
-- -----------------------------------------------------
CREATE TABLE tbl_events (
    eventID INT NOT NULL IDENTITY,
    eventName NVARCHAR(100) NOT NULL,
    PRIMARY KEY (eventID));

-- -----------------------------------------------------
-- Table tbl_objects
-- -----------------------------------------------------
CREATE TABLE tbl_objects (
    objectID INT NOT NULL IDENTITY,
    objectName NVARCHAR(100) NOT NULL,
    PRIMARY KEY (objectID));

-- -----------------------------------------------------
-- Table tbl_objects_has_tbl_events
-- -----------------------------------------------------
CREATE TABLE tbl_objects_has_tbl_events (
    objectID INT NOT NULL,
    eventID INT NOT NULL,
    PRIMARY KEY (objectID, eventID),
    CONSTRAINT fk_tbl_objects_has_tbl_events_tbl_objects
        FOREIGN KEY (objectID)
            REFERENCES tbl_objects (objectID)
                ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT fk_tbl_objects_has_tbl_events_tbl_events
        FOREIGN KEY (eventID)
            REFERENCES tbl_events (eventID)
                ON DELETE CASCADE ON UPDATE CASCADE);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

相权↑美人 2024-10-24 19:21:05

实体数据模型不显示仅包含 FK 的表。因此,在您的情况下,事件实体将具有导航属性对象,并且您的对象实体将具有导航属性事件。

因此,要获取您的信息,您可以编写如下查询:

IList dataList = (from dEvent in App.glidusContext.tbl_events
                  from dObject in dEvent.Objects
                    select new { dEvent.eventName, dObject.objectName}).ToList();

The entity data model doesn't show tables that only contain FK's. So in your case the Events entity will have a navigation property Objects and your Object entity will have a navigation property Events.

So to get your information you could write a query like this:

IList dataList = (from dEvent in App.glidusContext.tbl_events
                  from dObject in dEvent.Objects
                    select new { dEvent.eventName, dObject.objectName}).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文