-
C#教程之C#教程之c# List使用中遇到的问题
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
最近在项目上写的方法,想通过减少访问数据层,将需要重复调用的值存入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 }
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能