将三个DbSet连接到一个新的DbSet中

时间:2017-02-28 14:46:35

标签: c# asp.net linq model-view-controller asp.net-web-api

我正在尝试获取3个DbSet并取出其中的一些值并返回一个新的DbSet。我能够得到一个单独的房间,但我很难找到一组房间。

这是一间单人房,工作正常。

这是我的存储库调用。

    public Room GetRoom(Guid id)
    {
        var room = _db1Context.ClientRoom.FirstOrDefault(x =>x.Id == id);
        var roomDetails = _db2Context.RoomDetail.FirstOrDefault(x => x.ClientRoomId== id);
        var summary = _db3Context.RoomSummary.FirstOrDefault(x => x.ClientRoomId == id);

        var room = _roomFactory.BuildRoomEntity(room, roomDetails, summary);
        return room;
    }

这是我的工厂电话

public Room BuildRoomEntity(ClientRoom room, RoomDetail roomDetail, RoomSummary summary)
    {
        var room = new Room()
        {
            Id = room.ClientRoomId,                                                      
            Name = room.DisplayName,                          
            Description = roomDetail.Description,
            Status = summary.Status,
            OnlineStatus = summary.OnlineStatus,
            ServicePlan = roomdetail.ServicePlan
            Incidents = room.Incident == null ? new List<Incident>() : room.Incident.ToList(),
            Devices = roomDetail.Devices == null ? new List<Device>() : roomDetail.Devices.ToList()
        };

        return room;
    }

除了获取房间清单外,我想做类似的事情 这是我的回购电话的样子。

    public List<Room> GetRooms()
    {
        var room = _db1Context.room;
        var roomDetails = _db2Context.RoomDetails;
        var roomSummary = _db3Context.RoomSummaries;

        var room = _roomFactory.BuildRoomEntity(room, roomDetails, summary);
        return room;
    }

这就是我被困住的地方,我想使用Linq根据从其他dbsets传入的值创建Rooms列表

public List<Room> BuildRoomEntity(DbSet<ClientRoom> room, DbSet<RoomDetail> roomDetail, DbSet<RoomSummary> summary)
    {

        var rooms = new List<Room>()
        {

        }

        return rooms;
    }

由于

1 个答案:

答案 0 :(得分:0)

如果您的课程配置如下:

public class ClientRoom
{
    public int Id { get; set; }
    public string DisplayName { get; set; }
    public RoomDetail RoomDetail { get; set; }
    public RoomSummary RoomSummary { get; set; }
}

然后,您可以而且应该使用DbSet.Include

var room = _db1Context.room
room.Include(r=>r.RoomDetail);
room.Include(r=>r.RoomSummary);

如果您只在子表中有Ids,那么您可以使用连接:

var room = _db1Context.room;
var roomDetails = _db2Context.RoomDetails;
var roomSummary = _db3Context.RoomSummaries;

var subtables = roomDetails.Join(roomSummary, rd=>ClientRoomId, rs=>rs.ClientRoomId, (roomDetails, summary)=>new {roomDetails.ClientRoomId, roomDetails, roomSummary});

var rooms = room.Join(subtables, r=>r.Id, sub=>sub.ClientRoomId, (room, sub)=>
    new Room()
        {
            Id = room.Id,                                                      
            Name = room.DisplayName,                          
            Description = sub.roomDetail.Description,
            Status = sub.summary.Status,
            OnlineStatus = sub.summary.OnlineStatus,
            ServicePlan = sub.roomdetail.ServicePlan
            Incidents = room.Incident == null ? new List<Incident>() : room.Incident.ToList(),
            Devices = sub.roomDetail.Devices == null ? new List<Device>() : roomDetail.Devices.ToList()
        });