我有一个对象:
public class Ticket
{
public string EventName {get;set;}
public string Count {get;set;}
public string Price {get;set;}
... other properties;
}
对于测试:
var tickets = new List<Ticket>();
tickets.Add(new Ticket {EventName = "Test", TicketCount = 1, Price = 100});
tickets.Add(new Ticket { EventName = "Test", TicketCount = 2, Price = 200 });
tickets.Add(new Ticket { EventName = "Test2", TicketCount = 1, Price = 50 });
我想获得具有以下属性的匿名对象:EventName, TicketCount, Price
并按事件名称“分组”。
对于上面的示例,结果必须是:
匿名对象必须包含两条记录:
EventName = Test, TicketCount = 3, Price = 300
EventName = Test2, TicketCount = 1, Price = 50
我未完成的代码:
var groupedByEvent = tickets.GroupBy(t => t.EventName);
var obj = new {EventName = seatsByEvent.Select(t=>t.Key), TicketCount = seatsByEvent.Sum(???)}
如何解决这个问题?