首页 新闻 搜索 专区 学院

怎么优化下面那段代码,执行时间超长

0
悬赏园豆:20 [已关闭问题] 关闭于 2019-01-11 17:56

public ActionResult Deletead_agree_big(string id)
{
DirectResult result = new DirectResult();
result.Success = true;
ad_agree model = context.ad_agree.SingleOrDefault(x => x.agreeid == id);
ad_audit auModel = context.ad_audit.SingleOrDefault(x => x.agreeid == id);
if (model.auditstate != "01")
{
result.ErrorMessage = "当前订单审核状态已更改,无法删除,请刷新当页。";
result.Success = false;
return result;
}
if (auModel.time_1 != null)
{
result.ErrorMessage = "当前订单已审核过,无法删除。";
result.Success = false;
return result;
}
var agreeplay = context.ad_agreeplay.Where(x => x.agreeid == id).ToList();
var order = context.ad_order.Where(x => x.agreeid == id).ToList();
DateTime startTime = model.begindate;
DateTime endTime = model.enddate;
string advidType = model.advidtype.Trim();

        try
        {
            foreach (var item in order)
            {
                StringBuilder Sql = new StringBuilder();
                string nums = item.adposnumb.ToString();
                string accesscontrolid = item.devid;
                for (DateTime dt = new DateTime(Convert.ToDateTime(startTime).Year, Convert.ToDateTime(startTime).Month, Convert.ToDateTime(startTime).Day); dt <= new DateTime(Convert.ToDateTime(endTime).Year, Convert.ToDateTime(endTime).Month, Convert.ToDateTime(endTime).Day); dt = dt.AddDays(1))
                {
                    ad_scheduling sModel = context.ad_scheduling.SingleOrDefault(s => s.devid == accesscontrolid && s.stime == dt);
                    if (sModel != null)
                    {
                        int s = 0;
                        switch (advidType)
                        {
                            case "01":
                                s = Convert.ToInt32(sModel.traditionnum) - Convert.ToInt32(nums);
                                Sql.Append(" update  ad_scheduling set traditionnum = '" + s + "' where sid = '" + sModel.sid + "' ");
                                break;
                            case "02":
                                s = Convert.ToInt32(sModel.accuratenum) - Convert.ToInt32(nums);
                                Sql.Append(" update  ad_scheduling set accuratenum = '" + s + "' where sid = '" + sModel.sid + "' ");
                                break;
                            case "03":
                                s = Convert.ToInt32(sModel.welfarenum) - Convert.ToInt32(nums);
                                Sql.Append(" update  ad_scheduling set welfarenum = '" + s + "' where sid = '" + sModel.sid + "' ");
                                break;
                            //case "01":
                            //    sModel.traditionnum = sModel.traditionnum - Convert.ToInt32(nums);
                            //    break;
                            //case "02":
                            //    sModel.accuratenum = sModel.accuratenum - Convert.ToInt32(nums);
                            //    break;
                            //case "03":
                            //    sModel.welfarenum = sModel.welfarenum - Convert.ToInt32(nums);
                            //    break;
                        }
                        //context.Entry(sModel).State = EntityState.Modified;
                        //context.SaveChanges();
                    }
                }
                context.Database.ExecuteSqlCommand(Sql.ToString());
            }
            context.Database.ExecuteSqlCommand(" delete from ad_agreeplay where agreeid = '" + id + "' ");
            context.Database.ExecuteSqlCommand(" delete from ad_order where agreeid = '" + id + "' ");
            context.ad_audit.Remove(auModel);
            context.ad_agree.Remove(model);
            context.SaveChanges();
           
            //foreach (var item in agreeplay)
            //{
            //    context.ad_agreeplay.Remove(item);
            //}
            //foreach (var item in order)
            //{
            //    context.ad_order.Remove(item);
            //}

            return result;
        }
        catch (Exception ex)
        {

            ResourceManager.AjaxSuccess = false;
            ResourceManager.AjaxErrorMessage = ex.Message;
            result.ErrorMessage = ex.Message;
            result.Success = false;
            return result;
        }
    }
ly丶乐的主页 ly丶乐 | 初学一级 | 园豆:12
提问于:2019-01-11 17:29
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册