LINQ查询,连接表问题
我想编写一个查询,为每个事件 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实体数据模型不显示仅包含 FK 的表。因此,在您的情况下,事件实体将具有导航属性对象,并且您的对象实体将具有导航属性事件。
因此,要获取您的信息,您可以编写如下查询:
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: