如何减少迭代值到for循环的时间延迟 [英] how to reduce time delay in iterating values into for loop

查看:86
本文介绍了如何减少迭代值到for循环的时间延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中我有2000个值loiading值为for循环花费时间因此值迟到如何减少迭代值到for循环的时间延迟



< br $>








低于控制器代码

in database i have 2000 values loiading values in to for loop take time so values are getting late how to reduce time delay in iterating values into for loop






below code in controller

var cnt = values.Count(); List<AttributeDTO> lst = null;
                   List<AttributeDTO> lst1 = new List<AttributeDTO>();
                   List<pic_attr_value> pids = context.pic_attr_value.GroupBy(x => x.product_id)
                               .Select(g => g.FirstOrDefault())
                               .ToList();

                       foreach (var item in pids)
                   {


                       for (var i = 0; i < cnt; i++)

                       {

                           var productid = item.product_id;

                           var att = values[i]; var attl = values1[i];

                           var count = context.pic_attr_value.Count(a => a.attr_id == att && a.attr_lov_id == attl && a.product_id == productid);
                           if (count == 0) break;
                           if (i == cnt - 1)
                           {
                               var pavs = context.pic_attr_value.Where(a => a.attr_id == att && a.attr_lov_id == attl && a.product_id == productid).ToList();
                               lst = new List<AttributeDTO>();
                               foreach (var pav in pavs)
                               {
                                   var attr = new AttributeDTO();
                                   var st = context.pic_product.SingleOrDefault(a => a.product_id == pav.product_id);
                                   if (st != null) { attr.AttrName = st.part_number; attr.temp = st.part_description; }
                                   lst.Add(attr);
                               }


                           } if (lst != null && i == cnt - 1)
                           {
                               lst1.AddRange(lst);
                           }
                       }
                   }
                   return lst1;

推荐答案

var count = context.pic_attr_value.Count(a => a.attr_id == att && a.attr_lov_id == attl && a.product_id == productid);

var pavs = context.pic_attr_value.Where(a => a.attr_id == att && a.attr_lov_id == attl && a.product_id == productid).ToList();



以上两行有相同的查询。你可以把它做成一个。从结果中你可以得到如下的计数。




The above two lines has the same query. you can just make it as one. and from the result u can get the count as below.

var data = context.pic_attr_value.Where(a => a.attr_id == att && a.attr_lov_id == attl && a.product_id == productid).ToList();

int count = data.count();





而不是访问循环内的Context。你可以将上下文数据存储在局部变量中并尝试在循环中使用变量,这样可以节省一些时间。



instead of accessing the Context inside the loop. you can just store the context data in a local variable and try to use the variable inside the loop, it saves some time.

var localContext = context.pic_attr_value;


这篇关于如何减少迭代值到for循环的时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆