c# List使用中遇到的问题(转载:小牛导航,super-nb)

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

逻辑如下:

1、从数据库中获取AList(yycfList)

2、new一个BLis(_yycfList),将AList中的部分值赋予它

3、改变BList中的值

4、AList的值也变了

分享一个我个人的导航网站,小牛导航

小牛导航,爱再来一次(super-nb.com)

代码如下:

复制代码

 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 }

复制代码

更多NET精彩资源
链接:https://pan.baidu.com/s/1tmlqOFOoFZdWp2uuRHNRrQ 
提取码:fdeb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值