title: calculate shop discount from cart based on shop order limit in a multi-vendor e-commerce store in asp.net mvc5
body: <p>I have a multi-vendor e-commerce store in which every store manager can manage their discount on shop orders and items.the cart model is as below:</p>
  
  <pre><code>    namespace myapp.Models
       public class Cart
       {
          [Key]
          public int RecordId { get; set; }
          public string CartId { get; set; }
          public Guid ItemId { get; set; }
          public Guid StoreId { get; set; }
  
          public Decimal? ShopDiscount { get; set; }
          public long ShopId { get; set; }
          [Display(Name = "Discount Time")]
          public string Duration { get; set; }
          [StringLength(100, ErrorMessage = "Must be less than 100 characters")]
          public string StoreName { get; set; }
          public decimal? Giftback { get; set; }
          [Display(Name = "Min order For Disc")]
          public Decimal? OrderLimit { get; set; }
          public int Count { get; set; }
          public decimal? Discount { get; set; }
          public System.DateTime DateCreated { get; set; }
          public virtual Item Item { get; set; }
        }
      }
  </code></pre>
  
  <p>i have used entity-framework with code first scaffolding.
   moreover in my shoppingcart model class i have used shopdeal() method to calculate shoplevel discount but it returns always 0 and does. i think there is problem with this method.</p>
  
  <pre><code>    // shopdeal method to calculate shopdiscount for all items in cart 
       // 
  
              public decimal? ShopDeal()
          {
              decimal? shopdeal = 0;
             //get record with different shops so that calculate shop total 
       //separatly
              var results = db.Carts.Select(m =&gt; new { m.CartId, m.StoreId, 
      m.OrderLimit, m.ShopDiscount, m.Duration }).Distinct().ToList();
  
                // calculate total price of all items for a particular shop  
              foreach (var item in results)
              {
                  decimal? shoptotal = (from cartItems in db.Carts
                                        where cartItems.CartId == ShoppingCartId
                                        &amp;&amp; cartItems.Item.StoreId == 
      item.StoreId
                                        select 
      (decimal?)cartItems.Item.Price).Sum();
                  if (shoptotal &gt;= item.OrderLimit &amp;&amp; item.Duration == 
      "Started")
                  {
                      shopdeal = shopdeal + shoptotal * item.ShopDiscount / 100;
                  }
  
              }
  
              return shopdeal;
          }
  </code></pre>
  
  <p>in the above method we try to calculate total price of all items of a particular shop than compare shoptotal with shopdiscount  (orderlimit) of that shop and check its time period(started or not) if both conditions are true to apply shopdiscount for that shop .if the user buy from more than on shop than apply same if for all shop(usage of foreach loop).
   please help if  there is workaround to get price total for each shop and apply shopdiscount and get total shopdeal(total shopdiscount).the total function is as below :</p>
  
  <pre><code>        {
              // Multiply album price by count of that album to get 
              // the current price for each of those albums in the cart
              // sum all album price totals to get the cart total
              decimal? total = (from cartItems in db.Carts
                                where cartItems.CartId == ShoppingCartId
                                select (int?)cartItems.Count *
                                cartItems.Item.Price * (100 - 
                                cartItems.Item.Discount) / 100).Sum();
               // update total if shopdiscount available
              // why we need to apply shopdeal again? how to save result of 
               //shopdeal()
              total = total - ShopDeal(); 
              return total ?? decimal.Zero;
          }
  
   the above code works and no error is shown but the shopdiscount is always zero.any workaround to calculate and apply shopdiscount and reduce database queries.
  </code></pre>