我试图根据条件将对象分解为多个相同类型的对象。我怎样才能在C#中编写LINQ查询。
public class Order
{
public List<Driver> Drivers { get; set; }
public List<Vehicle> Vehicles { get; set; }
}
Order co = new Order();
例如,我的 co </ strong>对象有14个驱动程序和12个车辆。
我想创建Order类型的对象,它将包含5个驱动程序和4个车辆。
if (co.Drivers.count > 5 || co.vehicles.count > 4)
{
//Break the total number of Drivers and Vehicles into sets of 5 and 4 and add them to the Orde object.
}
由于 BB
答案 0 :(得分:2)
var newOrders = new List<Order>();
for (int drivers = 0, vehicles = 0;
drivers < co.Drivers.Count || vehicles < co.Vehicles.Count;
drivers += 5, vehicles += 4)
{
newOrders.Add(new Order {
Drivers = co.Drivers.Skip(drivers).Take(5),
Vehicles = co.Vehicles.Skip(vehicles).Take(4)
}));
}