带有子查询列表的LINQ查询

时间:2018-12-06 21:03:41

标签: asp.net-mvc linq

下面的查询应该从不同的表中选择一个信息列表,并且还应该带回与VehicleName返回的每个ID相关的db.Reservations s列表< / p>

该查询将带回数据,但是VehicleNames列表仅包含一条记录,并且该位置最多应带回5条记录,具体取决于为该特定实例保留了多少辆车。我尝试在VehicleName的{​​{1}}行中添加一个foreach,但我认为我使用的方式不正确。有人对我如何检索所需的值列表有任何想法吗?

select

编辑:更新的查询,仍然不确定如何获取已预订的车辆列表

 var query = from r in db.Reservations
             let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault() 
             let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).FirstOrDefault()
             let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
             let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
             select new
             {
                 StartDate = r.StartDate,
                 EndDate = r.EndDate,
                 Destination = r.Destination,
                 PurposeOfTrip = r.PurposeOfTrip,
                 TransportingStudents = r.TransportStudentsFG,
                 EmployeeName = e.FirstName + " " + e.LastName,                            
                 ApprovalStatus = rst.StatusType,
                 ThemeColor = r.ThemeColor,
                 VehicleName = (from v in db.Vehicles
                                where v.ID == rtv.VehicleID
                                select v.VehicleName).ToList()
              };

var query = from r in db.Reservations let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault() let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).Select(y => y.VehicleID).ToList() let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault() let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault() select new { StartDate = r.StartDate, EndDate = r.EndDate, Destination = r.Destination, PurposeOfTrip = r.PurposeOfTrip, TransportingStudents = r.TransportStudentsFG, EmployeeName = e.FirstName + " " + e.LastName, ApprovalStatus = rst.StatusType, ThemeColor = r.ThemeColor, VehicleName = (from v in db.Vehicles where v.ID == rtv.VehicleID select v.VehicleName).ToList() }; 不再存在,现在rtv.VehicleID返回ID列表。...我需要能够进入该列表以找到rtv在{ {1}} v.ID列表

1 个答案:

答案 0 :(得分:1)

看起来人们已经在评论中告诉过您,但这是应该起作用的代码:

var query = from r in db.Reservations
            let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault()
            let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).Select(y => y.VehicleID)
            let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
            let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
            select new
            {
                StartDate = r.StartDate,
                EndDate = r.EndDate,
                Destination = r.Destination,
                PurposeOfTrip = r.PurposeOfTrip,
                TransportingStudents = r.TransportStudentsFG,
                EmployeeName = e.FirstName + " " + e.LastName,
                ApprovalStatus = rst.StatusType,
                ThemeColor = r.ThemeColor,
                VehicleName = (from v in db.Vehicles
                              where rtv.Contains(v.ID)
                              select v.VehicleName).ToList()
            };

我还建议您查看您的实体之间的setting up navigation properties。您的查询可能会更简单,如下所示:

var query = from r in db.Reservations
            let e = r.Requestor
            let rs = r.ReservationStatus
            let rst = rs.ReservationStatusType
            select new
            {
                StartDate = r.StartDate,
                EndDate = r.EndDate,
                Destination = r.Destination,
                PurposeOfTrip = r.PurposeOfTrip,
                TransportingStudents = r.TransportStudentsFG,
                EmployeeName = e.FirstName + " " + e.LastName,
                ApprovalStatus = rst.StatusType,
                ThemeColor = r.ThemeColor,
                VehicleName = r.Vehicles.Select(v => v.VehicleName).ToList()
            };