-3

I have a list with tickets in it:

TicketID - Aantal (amount) - ActiePrijs (price)

For all those tickets the ActiePrijs (price) is still empty. But I also have a list with only those Actieprijs (prices). It's just a list of decimals.

Now I want to put the first actiePrijs from the decimal list into the ActiePrijs from the first ticket, the second ActiePrijs into the ActiePrijs of the second ticket etc.

I want to do it using linq method syntax.

Can someone help me?

Karan
  • 12,059
  • 3
  • 24
  • 40

2 Answers2

1

You dont show any code, but I thing you want something like this.

public class Properties
{
    public int Aantal { get; set; }
    public int Price { get; set; }
}
List<Properties> l = new List<Properties>();
l.Add(new Properties() { Aantal = 1 });
l.Add(new Properties() { Aantal = 2 });
l.Add(new Properties() { Aantal = 3 });
List<int> l2 = new List<int>();
l2.Add(1);
l2.Add(2);
l2.Add(3);
int index = 0;
l.ForEach(x => x.Price = l2[index++]);
Silny ToJa
  • 1,815
  • 1
  • 6
  • 20
1

I assume tickets & price lists are tickets and price.

You can use linq like below.

tickets = tickets.Select((ticket, index) => new Ticket 
                  { 
                      TicketID = ticket.TicketID,
                      Aantal = ticket.Aantal,
                      ActiePrijs = price[index]
                  })
                  .ToList();

Or if you have more properties and you do not want to create new object then use like below.

tickets = tickets.Select((ticket, index) =>
                  {
                      ticket.ActiePrijs = price[index];
                      return ticket;
                  })
                  .ToList();
Karan
  • 12,059
  • 3
  • 24
  • 40