简体   繁体   English

在Entity Framework中加入多对多表

[英]Joining many-to-many tables in Entity Framework

I have a table layout as shown in this image. 我有一个表格布局,如此图所示。 One main table ( User ), and two many-to-many tables ( Preference and Location ) with junction tables. 一个主表( User )和两个带有连接表的多对多表( PreferenceLocation )。 I have set up the correct relationships in the data model to allow selections from these m-2-m tables.... 我在数据模型中建立了正确的关系,允许从这些m-2-m表中进行选择....

在此输入图像描述

The report tool I'm writing allows the user to select (from a checklist) any user preferences or user locations. 我正在编写的报告工具允许用户(从清单中)选择任何用户首选项或用户位置。 What I'd like to do is choose only the records from the User table where the Preferences OR Locations contains at least one of their selections. 我想做的是只选择用户表中的偏好位置包含至少一个选项的记录。

Is this possible with a Linq query? 这可能是Linq查询吗? (I did previously do this in SQL, but it seemed easier to write in Linq until I got to this part!) (我之前在SQL中做过这个,但在我接触到这个部分之前,在Linq中写起来似乎更容易!)

Many thanks, 非常感谢,

EDIT: Visual Studio 2012, Entity Framework 4, SQL Server 2008 R2 编辑:Visual Studio 2012,实体框架4,SQL Server 2008 R2

from u in Users
where u.Locations.Any(l => l.Name == value) ||
      u.Preferences.Any(p => p.Title == value)
select u;

That will generate two EXISTS subqueries. 这将生成两个EXISTS子查询。 Lambda syntax: Lambda语法:

Users.Where(u => u.Locations.Any(l => l.Name == value) ||
                 u.Preferences.Any(p => p.Title == value));

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

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