patterncsharpMinor
Creating orders for gift transactions
Viewed 0 times
giftcreatingfortransactionsorders
Problem
Can this be improved?
foreach (Gift gift in usedGifts)
{
foreach (GiftTransaction GiftTransaction in gift.GiftTransactions)
{
if (!string.IsNullOrEmpty(giftTransaction.GiftId) &&
!orderList.Where(b => b.GiftId == giftTransaction.GiftId).Any())
{
Order order = OrderHelper.PopulateSingleOrder(orderRepository, sessionRepository, giftTransaction.GiftId);
if (order != null)
orderList.Add(order);
}
}
giftReferenceList.Add(gift.GiftReference);
}Solution
I'm not sure if it will make your query faster but here is my take on it:
If I read the code correctly you want to create orders for all gift transactions related to gift ids which are not covered by an order yet.
Unfortunately this requires an extra pass over the
If I read the code correctly you want to create orders for all gift transactions related to gift ids which are not covered by an order yet.
usedGifts.SelectMany(g => g.GiftTransactions)
.Select(gt => gt.GiftId)
.Where(gi => !string.IsNullOrEmpty(gi))
.Except(orderList.Select(o => o.GiftId)) // This will yield all gift ids which are not covered by an order
.Select(gi => OrderHelper.PopulateSingleOrder(orderRepository, sessionRepository, gi)) // generate the orders
.Where(o => o != null)
.ToList();Unfortunately this requires an extra pass over the
usedGifts list to add all the referencesgiftReferenceList.AddRange(usedGifts.Select(g => g.GiftReference));Code Snippets
usedGifts.SelectMany(g => g.GiftTransactions)
.Select(gt => gt.GiftId)
.Where(gi => !string.IsNullOrEmpty(gi))
.Except(orderList.Select(o => o.GiftId)) // This will yield all gift ids which are not covered by an order
.Select(gi => OrderHelper.PopulateSingleOrder(orderRepository, sessionRepository, gi)) // generate the orders
.Where(o => o != null)
.ToList();giftReferenceList.AddRange(usedGifts.Select(g => g.GiftReference));Context
StackExchange Code Review Q#38700, answer score: 7
Revisions (0)
No revisions yet.