简体   繁体   中英

How to compare object in foreach loop?

I'm currently doing a checking for item in cart. The item can be redeemed multiple time but is limited to certain number. Each item has an attribute redeem_count which store the number of time that it can be redeemed. So, below is my coding for checking the total number of each item redeemed.

var previousCoupon = "";
var currentCoupon = "";
int count = 0;
foreach (var p in cart.Promotions)
{
    var query = db.wmp_mst_mcp_promo
               .Where(a => a.wmp_mcp_promo_id == p.PromotionId)
               .Where(a => a.wmp_redeem_count != null && a.wmp_redeem_count > 0)
               .SingleOrDefault();

    if (query != null)
    {
        currentCoupon = query.wmp_mcp_promo_id;

        if (previousCoupon == currentCoupon)
            count++;
        else
            count = 0;

        previousCoupon = currentCoupon;

        if (count > query.wmp_redeem_count)
            result.Invalidate(string.Format("You are not allowed to redeem more than {0} \"{1}\" voucher in 1 order", query.wmp_redeem_count, query.wmp_descriptions));

As you can see, the coding works but only if the same item are redeemed in order. This might not valid because not all customer will redeem the item in order. Bug will occur if they redeem for example item A, then item B, and then item A again. It will work only if item A, item A then item B.

Edited

I want to store any item that have same ID and get only one of it attribute redeem_count so that I can do if statement on it. The redeem_count is different for each item.

Example

Item in cart

  • item A - 2 (redeem_count=2) Valid
  • item B - 2 (redeem_count=1) Invalid
  • item C - 3 (redeem_count=4) Valid

So it will throw the result.Invalidate

Any help? Thanks.

I would do something like (pseudocode):

var dict = new Dictionary<string /*item_Id*/, int /*count*/>;
// count redemptions for each id
foreach(var item in cart)
{
    if(dict.ContainsKey(item.Id))
         dict[item.Id]++;
    else
        dict.Add(item.Id, 1);
}

// check if any of them violate the allowed maximum
foreach( var itemId in dict.Keys)
{
    if(dict[itemId ] > GetMaxRedeemCount(itemId))
    {
        result.Invalidate(...);
        // you may want to break here...
        // break;
    }
}

Try this solution;

 var previousCoupon = "";
        var currentCoupon = "";
        int count = 0;
        foreach (var p in cart.Promotions)
        {
            var query = db.wmp_mst_mcp_promo
                .Where(a => a.wmp_mcp_promo_id == p.PromotionId)
                .Where(a => a.wmp_redeem_count != null && a.wmp_redeem_count > 0);
if (query != null)
{
       foreach(var queryItems in query)
              {
                currentCoupon = query.wmp_mcp_promo_id;

                if (previousCoupon == currentCoupon)
                    count++;
                else
                    count = 0;

                previousCoupon = currentCoupon;
            }
}
                if (count > query.wmp_redeem_count)
                    result.Invalidate(string.Format("You are not allowed to redeem more than {0} \"{1}\" voucher in 1 order", query.wmp_redeem_count, query.wmp_descriptions));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM