合并两个列表:我已经硬编码了两个列表,并希望打印条件匹配的列表数据.请让我知道我该怎么做?



合并两个列表:我已经硬编码了两个列表,并希望打印条件匹配的列表数据。请让我知道我该怎么做?

我尝试使用LINQ并得到了解决方案,但需要解决使用for循环,请给出相同的建议。

public IActionResult GetAllVehicleDrivers()
{
var VehicleList = new List<Vehicle>()
{
new Vehicle() { VehNum = "KA04AS1234", VehId = 1 },
new Vehicle() { VehNum = "KA04AS5689", VehId = 2 },
new Vehicle() { VehNum = "KA04AS9874", VehId = 3 },
new Vehicle() { VehNum = "KA04AS5647", VehId = 4 },
new Vehicle() { VehNum = "KA04AS7452", VehId = 5 },
};
var DriverList = new List<Driver>()
{
new Driver() { DriverName = "Gajaraj", DriverId = 123,  VehId = 1 },
new Driver() { DriverName = "Tajar", DriverId = 245,  VehId = 5 },
new Driver() { DriverName = "Pajara", DriverId = 363,  VehId = 3 },
new Driver() { DriverName = "Haja", DriverId = 425,  VehId = 2 },
new Driver() { DriverName = "Kaj", DriverId = 547,  VehId = 4 },
new Driver() { DriverName = "Dajarajines", DriverId = 547,  VehId = 1 },
};
foreach (var i in VehicleList)
{
foreach (var j in DriverList)
{
if (i.VehId == j.VehId)
{
List<dynamic> VehDriverList = new List<dynamic>()
{              i.VehId,
i.VehNum,
j.DriverId,
j.DriverName
};
return Ok(VehDriverList);
}
else
{
return NotFound();
}
}
}
}

}

似乎你的嵌套循环结构是不正确的,在循环内有返回语句,立即从动作方法返回,也实例化List<>在循环内部,一次又一次地覆盖前一个集合。那么,这个怎么样?

// initialize the List outside the nested loop
List<dynamic> VehDriverList = new List<dynamic>();
foreach (var i in VehicleList)
{
foreach (var j in DriverList)
{
if (i.VehId == j.VehId)
{
VehDriverList.Add(new List<dynamic>()
{
i.VehId,
i.VehNum,
j.DriverId,
j.DriverName
});
}
}
}
// return the results outside the loops
if (VehDriverList.Any())
{
return Ok(VehDriverList);
}
return NotFound();
}

所以你有VehiclesDrivers,每个Driver只驱动一个Vehicle,即外键VehicleId所指向的Vehicle。

理论上,同一辆车可以有更多的司机。例如,如果Driver[10]和Driver[11]都有一个外键VehicleId =[20]。在这种情况下,两个驾驶员都将驾驶车辆[20]。

在数据库术语中,您会说车辆和驾驶员之间存在一对多关系:每个车辆由零个或多个驾驶员驾驶,每个驾驶员只驾驶一个车辆,即外键所指的唯一一个车辆。

如果你有一个一对多的关系,比如客户和他们的零个或多个订单,学校和他们的学生,或者在你的例子中车辆和他们的司机,可能有两种查询:

  • 给我所有项目及其零个或多个子项;所有车辆,每辆车及其驾驶员
  • 给我子项,每个子项都有它的一个且唯一的父项;所有司机,每个司机和他驾驶的唯一一辆车。

在后一种情况下,您将使用Enumerable.Join的重载之一。在第一种情况下,您将使用enumerable . groupjoin的重载之一。

我几乎总是使用带有参数resultSelector的重载,这使我有机会精确地定义我想从驱动程序和车辆中获得的属性。

加入:司机,每个司机和他的车

var drivers = Drivers.Join(Vehicles, // Join Drivers and Vehicles
driver => driver.VehicleId,      // from every driver take the foreign key
vehicle => vehicle.Id,           // from every vehicle take the primary key
// parameter resultSelector: from every driver with his one and only
// matching vehicle, make one new:
(driver, vehicle) => new
{
// Select the Driver properties that you plan to use:
Id = driver.Id,
Name = driver.Name,
...
Vehicle = new
{
Id = vehicle.Id,
Type = vehicle.Type,
...
}
});

你不会得到没有司机的车辆。毕竟:您查询的是司机,而不是车辆!

我的建议是不要选择外键。您已经知道它等于Vehicle的主键。

GroupJoin: Vehicles,每个Vehicle有0个或多个Drivers

var vehiclesWithTheirDrivers= Vehicles.GroupJoin(Drivers
vehicle => vehicle.Id,           // from every vehicle take the primary key
driver => driver.VehicleId,      // from every driver take the foreign key
// parameter resultSelector: from every vehicle with its zero or more
// drivers, make one new:
(vehicle, driversOfThisVehicle) => new
{
// Select the Vehicle properties that you plan to use:
Id = vehicle.Id,
Type = vehicle.Type,
...
Drivers = driversOfThisVehicle.Select(driver => new
{
// Select the Driver properties that you plan to use:
Id = driver.Id,
Name = driver.Name,
...
// not needed, you already know the value:
// VehicleId = driver.VehicleId,
})
.ToList(),
});

相关内容

  • 没有找到相关文章

最新更新