Требуется получить список объектов DeliveryObjects, но только один на грузовик # со своим списком заказов вместо одной доставки на заказ.
Результат im gettting не проецирует сгруппированные заказы в одну доставку
void Main()
{
// project a list of orders into List<Delivery>
List<Order> olist = new List<UserQuery.Order>();
olist.Add(new Order{CustomerName = "Fred1", TargetDate=DateTime.Now.AddDays(-12), DeliveryWeight = 23.34M,DriverName = "Bob1", TruckNo="A1231" });
olist.Add(new Order{CustomerName = "Fred2", TargetDate=DateTime.Now.AddDays(-22), DeliveryWeight = 23.34M,DriverName = "Bob1", TruckNo="A1231" });
olist.Add(new Order{CustomerName = "Fred3", TargetDate=DateTime.Now.AddDays(-21), DeliveryWeight = 23.34M,DriverName = "Bob2", TruckNo="A1232" });
olist.Add(new Order{CustomerName = "Fred4", TargetDate=DateTime.Now.AddDays(-2), DeliveryWeight = 23.34M,DriverName = "Bob3", TruckNo="A1233" });
olist.Add(new Order{CustomerName = "Fred5", TargetDate=DateTime.Now.AddDays(-13), DeliveryWeight = 23.34M,DriverName = "Bob4", TruckNo="A1234" });
olist.Add(new Order{CustomerName = "Fred6", TargetDate=DateTime.Now.AddDays(-20), DeliveryWeight = 23.34M,DriverName = "Bob4", TruckNo="A1234" });
olist.Add(new Order{CustomerName = "Fred7", TargetDate=DateTime.Now.AddDays(-11), DeliveryWeight = 23.34M,DriverName = "Bob5", TruckNo="A1235" });
olist.Add(new Order{CustomerName = "Fred8", TargetDate=DateTime.Now.AddDays(-10), DeliveryWeight = 23.34M,DriverName = "Bob6", TruckNo="A1236" });
olist.Add(new Order{CustomerName = "Fred9", TargetDate=DateTime.Now.AddDays(-8), DeliveryWeight = 23.34M,DriverName = "Bob6", TruckNo="A1236" });
olist.Dump();
List<Delivery> delvlist = (from a in olist
group a by new {a.TruckNo}
into b
from c in b
select new Delivery()
{
RefNo = c.TruckNo,
orders = b.ToList()
}).ToList();
delvlist.Dump();
}
// Define other methods and classes here
public class Delivery {
public string RefNo { get; set; }
public List<Order> orders { get; set;}
}
public class Order{
public string CustomerName { get; set; }
public DateTime TargetDate { get; set; }
public Decimal DeliveryWeight { get; set; }
public string DriverName { get; set; }
public string TruckNo { get; set; }
}
[править] Я думаю, это понятно?
List<Delivery> delvlist = (from a in olist
group a by new {a.TruckNo}
into b
select new Delivery()
{
RefNo = b.Key.TruckNo,
orders =
(from c in b select new Order()
{
CustomerName = c.CustomerName,
TargetDate = c.TargetDate
})
.ToList()
}).ToList();