【问题标题】:Database not updating with loop数据库不循环更新
【发布时间】:2016-04-27 05:42:26
【问题描述】:

我必须根据用户输入的百分比将客户分配给不同的员工。例如,我有 10 个客户属于 salesman3,我为 salesman1 输入 50%,为 salesman2 输入 50%。这意味着这些客户中有 5 个去 salesman1,另外 5 个去 salesman2。我想通过更新数据库中的推销员 ID 来做到这一点。我已经放置了一些计数器以确保它正在输入百分比并且它正在循环遍历整个销售人员列表。它证明它正在运行整个 foreach 循环并获得百分比,但是当我检查数据库时,它只更新一次。这是我拥有的代码。如果我这里没有东西,我很抱歉。我是新用户和初学者编码器。谢谢。

public ActionResult Submit(string json, string terminated_id, string total_clients)
{
    int T_ID = Convert.ToInt32(terminated_id);
    int T_clients = Convert.ToInt32(total_clients);
    List<emp_info> list = JsonConvert.DeserializeObject<List<emp_info>>(json);
    int counter = 0;
    foreach (var obj in list)
    {
        counter++;
    }
    int counting = 0;
    string[] array = new string[counter];
    int i = 0;
    foreach (var obj in list)
    {
        counting++;
        array[i] = obj.percent;
        i++;
        if (obj.percent != "")
        {
            int count = Convert.ToInt32((double)T_clients * (Convert.ToDouble(obj.percent) / 100));
            var selected_list = db.Clients.ToList().Take(count);
            var query = from cust in selected_list
                        where cust.Salesperson_ID == T_ID
                        select cust;
            foreach (Client cust in query)
            {
              int int_id =  Convert.ToInt32(obj.ID);
                cust.Salesperson_ID = int_id;
                db.SaveChanges();
            }
            //db.SaveChanges();
            //return View();
        }
    }
    ViewBag.counting = counting;
    ViewBag.percentages = array;
    ViewBag.counter = counter;
    db.SaveChanges();
    return View();
}

【问题讨论】:

  • 只更新一次是什么意思?
  • 例如,如果我必须分配属于 salesperson3 的 10 个客户,并且如果我将 50% 用于 salesperson1,将 50% 用于 salesperson2,那么它只会将前 5 个客户更新为 salesperson1,而其他 5 个将继续分配给salesperson3。
  • 调试时'list'中有多少对象?
  • 但是如果count的值为5,那么只有这5个会被更新(不是10个)。
  • 您这里有一些糟糕的代码,而且只需不到您所展示的一半即可完成。没有时间知道,但我会在一个小时左右添加一个答案。首先将 (string json, string terminated_id, string total_clients) 更改为 (List&lt;emp_info&gt; json, int terminated_id, int total_clients) 并删除前 3 行代码。要获取counter,请使用int counter = json.Count;。但是所有这些都表明您的视图存在其他问题,并且它们将数据发送到控制器。

标签: c# asp.net-mvc linq


【解决方案1】:

你总是用这个db.Clients.ToList().Take(count);吸引第一批客户 试试var selected_list = db.Clients.ToList().Skip((counting-1) * count).Take(count);
(counting-1)从 0 开始,所以一开始不会跳过任何客户,对于下一个员工,您将跳过前 5 个客户(在您的情况下)。该代码未经测试,但请尝试一下。我想发表评论,但我没有足够的代表:)

【讨论】:

    【解决方案2】:

    通过实际采用您想要的而不是通用的 json 和字符串然后转换它们来简化您的 API。

    1. 在一次数据库调用中获取您要更新的所有记录。
    2. 创建一个包含 ID 和上限的新数组 百分比(合计)是他们的。 {{Name="Bob",ID=1,percent=5},{Name="John",ID=2,percent=10},{Name="Joe",ID=3,percent=85}} 变为{{ID=1,percent=5},{ID=2,percent=15},{ID=3,percent=100}}
    3. 循环遍历数组和 分配正确的销售人员。
    4. 更新数据库。

    像这样:

    public ActionResult Submit(List<emp_info> list, int terminated_id)
    {
      // Grab records to update in one call
      var clients=db.Clients.Where(c=>c.Salesperson_ID==terminated_id).ToList();
      var total=clients.Count();
    
      if (clients.Sum(c=>c.percent)!=100)
        throw new ApplicationException("Percentages must add up to 100%!");
    
      // Create new array with upperbound percent
      var newlist=list
        .Aggregate(new {ID=0,percent=0},(prev,curr)=>new {
          ID=curr.ID,percent=prev.percent+curr.percent
        });
      // Sanity check
      Debug.Assert(newlist.Last().percent==100);
    
      // Loop through records and assign new salesperson
      for(var x=0;x<total;x++)
      {
        clients[x].Salesperson_ID=newlist.Last(l=>l.percent<=x*100/total).ID;
      }
    
      // Send updates to database
      db.SaveChanges();
      ViewBag.counting = total;
      ViewBag.percentages = list.Select(l=>l.percent).ToArray();
      ViewBag.counter = total;
      return View();
    }
    

    【讨论】:

    • 你也可以通过join来完成,但这有点复杂。
    猜你喜欢
    • 2010-10-08
    • 2012-12-30
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    相关资源
    最近更新 更多