I have tried to sort this out myself but I am having to do it in VB which confuddles me even more. basically I have a lit of objects
List<Pax> paxes;
one of the Pax properties is Voucher, which in turn has some properties such as ID, and Price
what I want to do is select out of a list of Pax, a list of Vouchers and their cumulative prices
so from
Pax1 - Voucher{ VoucherDesc = 10 percent off, VoucherCode = 10-OFF, Price = £150}
Pax2 - Voucher{ VoucherDesc = 10 percent off, VoucherCode = 10-OFF, Price = £120}
Pax3 - Voucher{ VoucherDesc = Buy one get one free, VoucherCode = BOGOF, Price = £300}
to
{10 percent off, 10-OFF, £270 }
{Buy one get one free, BOGO, £300 }
I am sure this is possible with a bit of linq and grouping, but I am completely stumped
presume it is along the lines of
dim newlist = from p in paxes group p by p.VoucherCode into g _
select new With {g. ..}
yes this has to end up in VB but as I find that rather painful, figured I would make more sense of it in C# however, as the VB syntax is pure evil, if someone could post both, I might finally be able to make sense of what its doing. as the code converter doesn't seem to play nicely with extended linq queries.
any help much appreciated
thanks for the replies
managed to convert that to vb thus
Dim q = From v In paxes.Select(Function(p) p.Voucher).Where(Function(v) v IsNot Nothing) _
Group v By v.OptionID, v.VoucherCode, v.Description Into Group _
Select OptionID, VoucherCode, Description, TotalDiscount = Group.Sum(Function(v) v.Price)
fields are named a little different but it works fine helped by lazyb's post and this link: http://msdn.microsoft.com/en-us/vstudio/bb688088.aspx
Assume price is a decimal or double (not string):
var query = from v in paxes.Select(p => p.Voucher)
group v by new {v.VoucherDesc, v.VoucherCode } into g
select new {
Desc = g.Key.VoucherDesc,
Code = g.Key.VoucherCode,
Total = g.Sum(x => x.Price)
};
Update: you question was initially tagged with C#, thus this is a C# solution. Hope you can translate it to VB. If price is string, then just add parsing of value. E.g.
Total = g.Sum(x => Double.Parse(x.Price.Replace("£", ""))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With