c# - 通过 Entity Framework 6 中另一个表中的外键获取记录

标签 c# sql-server entity-framework linq entity-framework-6

我需要将所有座位附加到特定预订。

我有这些类(class):

public class Seat
{
    public Guid Id { get; set; }
    public string RowNumber { get; set; }
    public int SeatNumber { get; set; }
}

public class ReservationSeat
{
    public Guid Id { get; set; }
    public Guid ReservationId { get; set; }
    public Guid SeatId { get; set; }

    public Reservation Reservation { get; set; }
    public Seat Seat { get; set; }
}

我已经尝试使用此 linq to entities 语句,但没有成功。它似乎从 seats 表中返回所有的座位。

public static List<Seat> GetSeatsForReservation(Guid reservationId)
{
    using (var db = new EntityContext())
    {
        return db.Seats.Where(s => db.ReservationSeat
                                     .Select(rs => rs.ReservationId)
                                     .Contains(reservationId)).ToList();
    }
}

最佳答案

尝试:

public static List<Seat> GetSeatsForReservation(Guid reservationId)
    {
        var db= new  EntityContext();
        return (from s in db.ReservationSeat
                where s.ReservationID==Guid
                select s.seat).ToList();
    }

关于c# - 通过 Entity Framework 6 中另一个表中的外键获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37208329/

相关文章:

c# - 停止接收来自 SubscriptionClient 的消息

c# - 是什么污染了这个 switch 语句?

c# - 尝试连接到 .sdf 数据库时出现异常

entity-framework - Entity Framework - 我的对象上下文在哪里?

asp.net - 如何将成员资格表与 Entity Framework 集成? ASP.net

c# - 将字符串作为日期时间插入 SQL 中,无需 CAST

c# - 分离 int 和 char 并生成整数

sql-server - SQL Server : how to populate sparse data with the rest of zero values?

sql - SSIS 包 - 由于错误 0xC0014062,无法加载包

c# - 无法创建对象上下文