使用实体框架过滤数据

时间:2014-10-23 23:56:14

标签: sql entity-framework linq entity-framework-6

我正在尝试获取正确的数据

客房表

Id | Name
1    Room1
2    Room2

资源表

Id | Name
1    Resource1
2    Resource2
3    Resource3

RoomResources表

Id | RoomId | ResourceId
1      1         1
2      1         2
3      1         3
4      2         2
5      2         3

我想选择一个包含Resource1和Resource2的房间 我正在使用此代码

int[] ids = sResources.Split(',').Select(s => int.Parse(s)).ToArray();

rooms = from r in context.Rooms
    where r.Area.Office.Id == officeId
    && r.MaximumPeople >= numberOfPeople
    && r.RoomResources.Any(s => ids.Contains(s.ResourceId))
    select r;

但它返回Room1和Room2,结果应为Room1

1 个答案:

答案 0 :(得分:0)

也许这个?

int[] ids = sResources.Split(',').Select(s => int.Parse(s)).ToArray();

rooms = from r in context.Rooms
    where r.Area.Office.Id == officeId
    && r.MaximumPeople >= numberOfPeople
    && ids.All(i => r.RoomResources.Any(s => s.ResourceId == i)) // try this here
    select r;