【问题标题】:filter data with entity framework使用实体框架过滤数据
【发布时间】:2014-12-19 18:58:46
【问题描述】:

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

客房桌

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

【问题讨论】:

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


    【解决方案1】:

    也许是这个?

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-18
      • 2019-04-14
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      相关资源
      最近更新 更多