在C#LINQ中获取具有相同外键的多列数据

时间:2017-06-26 07:34:43

标签: c# linq

我有两个表,第一个是ReservationVehicleDetail = RVD,第二个是Employee = EMP

在RVD中有两列PrimaryDriverId和SecondaryDriverId都映射到EMP表的EmployeeId

我的问题是如何使用这两个ID来获取Employee表的数据。

这是我的代码:

 vm.BookVehicleDetailList = db.ReservationVehicleDetails.Where(x => x.ReservationId == vm.ReservationId && x.Status == Constant.ACTIVE).Select(v => new BookVehicleDetailModel()
                        {
                            VehicleImageURL = v.Vehicle.PictureURL,
                            IsDutySlipClosed = v.IsDutySlipClosed.HasValue ? v.IsDutySlipClosed.Value : false,
                            ReservationVehicleDetailId = v.ReservationVehicleDetailIId,
                            VehicleId = v.VehicleId.HasValue ? v.VehicleId.Value : 0,
                            VehicleCategory = v.VehicleCategory,
                            IsAC = v.IsAC,
                            ACorNonAC = v.IsAC ? "AC" : "Non AC",
                            FuelType = v.FuelType,
                            SeatingCapacity = v.Capacity,
                            RegistrationNumber = v.RegistrationNumber,
                            PrimaryDriverId = v.PrimaryDriverId.HasValue ? v.PrimaryDriverId.Value : 0,
                            PrimaryDriverName = v.PrimaryDriverId.HasValue ? v.Employee.FirstName : v.PrimaryDriverName,
                            SecondaryDriverId = v.SecondaryDriverId.HasValue ? v.SecondaryDriverId.Value : 0,
                            SecondaryDriverName = v.SecondaryDriverId.HasValue ? v.Employee.FirstName : v.SecondaryDriverName,
                            TariffType = v.TariffType,
                            DailyMinimumDistance = v.DailyMinimumDistance.HasValue ? v.DailyMinimumDistance.Value : 0,
                            TariffPerUnitDistance = v.TariffPerUnitDistance.HasValue ? v.TariffPerUnitDistance.Value : 0,
                            ExternalCompanyName = v.ExternalCompanyName,
                            StartDistanceReading = v.StartDistanceReading.HasValue ? v.StartDistanceReading.Value : 0,
                            EndDistanceReading = v.EndDistanceReading.HasValue ? v.EndDistanceReading.Value : 0,
                            GarageDistanceReading = v.GarageDistanceReading.HasValue ? v.GarageDistanceReading.Value : 0,
                            GrossTariff = v.GrossTariff.HasValue ? v.GrossTariff.Value : 0,
                            Distance = (v.StartDistanceReading.HasValue ? v.StartDistanceReading.Value : 0) + " - " + (v.EndDistanceReading.HasValue ? v.EndDistanceReading.Value : 0) + " - " + (v.GarageDistanceReading.HasValue ? v.GarageDistanceReading.Value : 0) + " (" + ((v.EndDistanceReading.HasValue ? v.EndDistanceReading.Value : 0) - (v.StartDistanceReading.HasValue ? v.StartDistanceReading.Value : 0) + (v.GarageDistanceReading.HasValue ? v.GarageDistanceReading.Value : 0)) + " KM)",
                        }).ToList();

现在基于第一个PrimaryDriverId的数据返回意味着PrimaryDriverId和SecodaryDriverId都不同,但是在执行查询后,PrimaryDriverName和SecondaryDriverName都是相同的。

那么如何解决这个问题。

2 个答案:

答案 0 :(得分:1)

您的v.employee仅与一名员工有关。所以你得到了相同的Employee对象。

我假设你的模型看起来像这样:

public class RVD
{
    public int Id { get; set; }
    public Employee Employee { get; set; } //This will only link to one Employee
    public int PrimaryDriverId { get; set; }
    public int SecondaryDriverId { get; set; }
}

您可以查看更改Linq以添加联接,以便将RVD映射到两个不同的员工:

e.g。

var result = (from r in rvds
                        join e1 in emps on r.PrimaryDriverId equals e1.Id
                        join e2 in emps on r.SecondaryDriverId equals e2.Id
                        where r.Id == 1
                        select new
                        {
                            PrimaryDriverName = e1.Name,
                            SecondaryDriverName = e2.Name
                        }).ToList();

或者您可以更新您的模型。

e.g。

public class RVD
{
    public int Id { get; set; }
    public Emp PrimaryDriver { get; set; }
    public Emp SecondaryDriver { get; set; }
}

public class Emp
{
    public int Id { get; set; }
    public string Name { get; set; }
}

然后你可以这样做:

var emp1 = new Emp() { Id = 1, Name = "John" };
var emp2 = new Emp() { Id = 2, Name = "Dave" };

var rvd = new RVD() { Id = 1, PrimaryDriver = emp1, SecondaryDriver = emp2 };

List<RVD> rvds = new List<RVD>();
rvds.Add(rvd);


var result  = rvds.Where(x => x.Id == 1).Select(x => new
{
    PrimaryDriverName = x.PrimaryDriver.Name,
    SecondaryDriverName = x.SecondaryDriver.Name,

}).ToList();

答案 1 :(得分:0)

我猜你可能错了。

PrimaryDriverName = v.PrimaryDriverId.HasValue ? v.Employee.FirstName : v.PrimaryDriverName,

SecondaryDriverName = v.SecondaryDriverId.HasValue ? v.Employee.FirstName : v.SecondaryDriverName

我想你想要在Id存在的情况下分配驱动程序名称,否则你想要分配Employee.FirstName,但是当你有Id's时,你似乎正在分配Employee.FirstName对于主驾驶员和辅助驾驶员都是如此。

我猜您应该尝试按以下方式切换条件。

PrimaryDriverName = v.PrimaryDriverId.HasValue ? v.PrimaryDriverName : v.Employee.FirstName,

SecondaryDriverName = v.SecondaryDriverId.HasValue ? v.SecondaryDriverName : v.Employee.FirstName,