Reputation: 162
I have 3 lists lstTicketType
, lstQuantity
lstAmount
. I am trying to merge ticket types and update the other two lists accordingly
The data is as follows
lstTicketType | lstQuantity | lstAmount
---------------------------------------
Standard | 1 | 8:5
Standard | 1 | 8.5
Student | 1 | 6.5
Student | 1 | 6.5
Senior | 1 | 4
I am able to find the duplicates by using
var query = lstTicketType.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => new { Counter = y.Count() })
.ToList();
how do i update the list so that it looks like
lstTicketType | lstQuantity | lstAmount
---------------------------------------
Standard | 2 | 17
Student | 2 | 13
Senior | 1 | 4
Upvotes: 1
Views: 94
Reputation: 59
hi your answer is the under code
List<string> lstTicketType = new List<string>() { "Standard", "Standard", "Student", "Student", "Senior" };
List<int> lstQuantity = new List<int>() { 1,1,1,1,1};
List<double> lstAmount = new List<double>() {8.5 , 8.5 , 6.5 , 6.5 ,4};
List<mergeClass> LstMC = new List<mergeClass>();
for (int i = 0; i < lstTicketType.Count(); i++)
{
mergeClass NewMC = new mergeClass(lstTicketType[i], lstQuantity[i], lstAmount[i]);
LstMC.Add(NewMC);
}
var query=LstMC.GroupBy(q=>q.TicketType)
.Select(q=>new{TicketType=q.First().TicketType,
Quantity=q.Sum(a=>a.Quantity),
Amount=q.Sum(a=>a.Amount)
}).ToList();
and you must create under class in your project
public class mergeClass
{
public string TicketType { set; get; }
public int Quantity { set; get; }
public double Amount { set; get; }
public mergeClass(string T, int Q, double A)
{
TicketType = T;
Quantity = Q;
Amount = A;
}
}
Upvotes: 0
Reputation: 525
List<Ticket> tickets = new List<Ticket>()
{
new Ticket(){
TicketType="Standard",
Quantity =1,
Amout= 8.5M
},
new Ticket(){
TicketType="Standard",
Quantity =1,
Amout= 8.5M
},
new Ticket(){
TicketType="Student",
Quantity =1,
Amout= 6.5M
},
new Ticket(){
TicketType="Student",
Quantity =1,
Amout= 6.5M
},
new Ticket(){
TicketType="Senior",
Quantity =1,
Amout=4M
},
};
var groupTicket = tickets.GroupBy(p => p.TicketType).Select(t => new
{
TicketType = t.Key,
Quantity = t.Sum(x => x.Quantity),
Amount = t.Sum(x => x.Amout)
}).ToList();
Upvotes: 0
Reputation: 6965
Try using "Sum". Don't rely on one my answer and try to write your own code to understand how it works. Otherwise, you will never know how to code. This is my reference - https://stackoverflow.com/a/16522841/1554116
internal class Ticket
{
public string TicketType { get; set; }
public int Quantity { get; set; }
public decimal Amount { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
var list = new List<Ticket>();
list.Add(new Ticket() { TicketType = "Standard", Quantity = 1, Amount = 8.5m });
list.Add(new Ticket() { TicketType = "Standard", Quantity = 1, Amount = 8.5m });
list.Add(new Ticket() { TicketType = "Student", Quantity = 1, Amount = 6.5m });
list.Add(new Ticket() { TicketType = "Student", Quantity = 1, Amount = 6.5m });
list.Add(new Ticket() { TicketType = "Senior", Quantity = 1, Amount = 4m });
foreach (var item in list)
{
System.Console.WriteLine($"TicketType: {item.TicketType}, Quantity: {item.Quantity}, Amount: {item.Amount}");
}
System.Console.WriteLine("------------------------------------------");
list = list.GroupBy(x => x.TicketType).Select(x => new Ticket()
{
TicketType = x.First().TicketType,
Quantity = x.Sum(y => y.Quantity),
Amount = x.Sum(y => y.Amount)
}).ToList();
foreach (var item in list)
{
System.Console.WriteLine($"TicketType: {item.TicketType}, Quantity: {item.Quantity}, Amount: {item.Amount}");
}
System.Console.Read();
}
}
Upvotes: 3