如何在Asp.Net中使用Linq for For循环进行时间优化 [英] How Do I Use Linq In For Loop In Asp.Net For Time Optimization

查看:235
本文介绍了如何在Asp.Net中使用Linq for For循环进行时间优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的代码如下,并希望实施Linq进行时间优化。

Hi All,

I have code as below and want to implement Linq for time optimization.

if (RadTreeViewCustodian.Nodes[0] != null)
          {
              var node = "";
              for (int i = 0; i < RadTreeViewCustodian.Nodes[0].Nodes.Count; i++)
              {
                  if (RadTreeViewCustodian.Nodes[0].Nodes[i].Text != null)
                  {
                      node =
                          RadTreeViewCustodian.FindNodeByText(RadTreeViewCustodian.Nodes[0].Nodes[i].Text).ToString();
                  }
                  else
                  {
                      node =
                          RadTreeViewCustodian.FindNodeByValue(RadTreeViewCustodian.Nodes[0].Nodes[i].Value)
                                              .ToString();
                  }
                  if (RadTreeViewCustodian.Nodes[0].Nodes[i].Checked == true)
                  {
                      RadTreeViewCustodian.Nodes[0].Nodes[i].Checked = false;

                  }
                  else
                  {
                      RadTreeViewCustodian.Nodes[0].Nodes[i].Checked = true;
                  }
              }
          }







请帮助




Please help

推荐答案

LINQ是一种编译器功能。它与处理时间无关。当然,如果您的迭代逻辑不正确会更快。



现在在您的代码中,



1.你为什么这样做?



LINQ is a compiler feature. It has nothing to do to processing time. Of course it would be quicker if your iteration logic is not proper.

Now in your code,

1. Why do you even do this?

if (RadTreeViewCustodian.Nodes[0].Nodes[i].Text != null)
                 {
                     node =
                         RadTreeViewCustodian.FindNodeByText(RadTreeViewCustodian.Nodes[0].Nodes[i].Text).ToString();
                 }
                 else
                 {
                     node =
                         RadTreeViewCustodian.FindNodeByValue(RadTreeViewCustodian.Nodes[0].Nodes[i].Value)
                                             .ToString();
                 }



您已经拥有一个节点对象,并且您再次在集合中找到它。不需要。



2.这也不准确。




You already have a node object and you are again finding it in the collection. Not required.

2. This is also not accurate.

if (RadTreeViewCustodian.Nodes[0].Nodes[i].Checked == true)
                  {
                      RadTreeViewCustodian.Nodes[0].Nodes[i].Checked = false;

                  }
                  else
                  {
                      RadTreeViewCustodian.Nodes[0].Nodes[i].Checked = true;
                  }





你只需要这里的if块而不是else。



You just need the if block here and not the else.


这篇关于如何在Asp.Net中使用Linq for For循环进行时间优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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