简体   繁体   English

如何在foreach循环中比较对象?

[英]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. 每个项目都有一个属性redeem_count,该属性存储可以兑换的时间。 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. 如果他们兑换例如项目A,然后项目B,然后再次项目A,则会发生错误。 It will work only if item A, item A then item B. 它仅在项目A,项目A然后项目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. 我想存储具有相同ID的任何项目,并只获得其中一个属性redeem_count,以便可以在其上执行if语句。 The redeem_count is different for each item. 每个项目的redeem_count不同。

Example

Item in cart 购物车中的物品

  • item A - 2 (redeem_count=2) Valid 项目A-2(redeem_count = 2) 有效
  • item B - 2 (redeem_count=1) Invalid 项目B-2(redeem_count = 1) 无效
  • item C - 3 (redeem_count=4) Valid 项目C-3(redeem_count = 4) 有效

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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