当前位置: 移动技术网 > IT编程>开发语言>.net > c# List使用中遇到的问题

c# List使用中遇到的问题

2019年03月23日  | 移动技术网IT编程  | 我要评论

重庆小面协会成立,雪之梦下载,中国艺术玻璃网

最近在项目上写的方法,想通过减少访问数据层,将需要重复调用的值存入list,无意中碰到的一个巨坑,至今仍不明所以,在此写出来,一来是看看有没有同道中人,二来是看看有没有大牛能解惑。

逻辑如下:

1、从数据库中获取alist(yycflist)

2、new一个blis(_yycflist),将alist中的部分值赋予它

3、改变blist中的值

4、alist的值也变了

 

代码如下:

 1 list<a> alist = getalist();
 2 list<x> xlist = getxlist();
 3 list<y> ylist = getylist();
 4 foreach (var y in ylist)
 5 {
 6     list<a> blist = alist.findall(x => x.customtype == y.customtype);
 7     if (y.isprivate || y.issustainable)
 8     {
 9         foreach (var b in blist)
10         {
11             x _x = xlist.find(x => x.curve == b.curve &&
12                                    x.customtype == b.customtype &&
13                                    x.fixtype == (y.issustainable ? 2 : 1));
14             if (_x != null)
15             {
16                 
17                 b.afactor = b.afactor + _x.drange / 100;    //此处修改后直接影响alist的值
18                 b.cfactor = b.cfactor + ((_x.urange - _x.drange) / 5 / 100);
19             }
20         }
21     }
22 }

 

最终解决代码:

 1 list<a> alist = getalist();
 2 list<x> xlist = getxlist();
 3 list<y> ylist = getylist();
 4 foreach (var y in ylist)
 5 {
 6     list<a> blist = alist.findall(x => x.customtype == y.customtype);
 7 
 8     if (y.isprivate || y.issustainable)
 9     {
10         blist = new list<a> ();  //此处重新new blist
11         foreach (var b in alist.findall(x => x.customtype == y.customtype))
12         {
13             x _x = xlist.find(x => x.curve == b.curve &&
14                                    x.customtype == b.customtype &&
15                                    x.fixtype == (y.issustainable ? 2 : 1));
16             b _b = new b();  //此处new一个b
17             if (_x != null)
18             {
19                 _b.afactor = b.afactor + _x.drange / 100;
20                 _b.bfactor = b.bfactor;
21                 _b.cfactor = b.cfactor + ((_x.urange - _x.drange) / 5 / 100);
22                 blist.add(_b);  //将改变后的b加入list
23             }
24             else
25             {
26                 blist.add(b);   //无需改变则将旧b加入list
27             }
28         }
29     }
30 }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网