Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在两个元素连接之前提到列表的大小_C#_Linq - Fatal编程技术网

C# 如何在两个元素连接之前提到列表的大小

C# 如何在两个元素连接之前提到列表的大小,c#,linq,C#,Linq,看看我的代码是怎么写的。在这里,我连接两个数据表,并将结果集存储到列表中 resultset存储在BogeyConfigList列表中,但我听说当数据存储在其中时,列表中会有许多空项,这些空项会消耗内存 所以,在加入之前,我如何声明具有适当大小的列表作为结果,加入结果集可以完全存储到列表中 请指导我如何处理这种情况。在我的例程中,我喜欢一系列的多个联接,其中我不能提到列表大小,因为我事先不知道在结果集中联接之后会有多少数据。讨论处理我可以节省一些内存的场景的最佳方法 谢谢如果您知道在使用构造函数

看看我的代码是怎么写的。在这里,我连接两个数据表,并将结果集存储到列表中

resultset存储在BogeyConfigList列表中,但我听说当数据存储在其中时,列表中会有许多空项,这些空项会消耗内存

所以,在加入之前,我如何声明具有适当大小的列表作为结果,加入结果集可以完全存储到列表中

请指导我如何处理这种情况。在我的例程中,我喜欢一系列的多个联接,其中我不能提到列表大小,因为我事先不知道在结果集中联接之后会有多少数据。讨论处理我可以节省一些内存的场景的最佳方法

谢谢

如果您知道在使用构造函数向列表中添加项目时,为了节省内存和提高效率,您可以使用初始容量初始化列表

如果您不知道最终容量是多少,但仍希望保留尽可能多的内存,则可以在将项目添加到列表后调用。这将强制列表的容量与计数匹配

 List<QCHelper> BogeyConfigList = null;
 BogeyConfigList = (from bogyconfiglist in Bogeylist.AsEnumerable().AsParallel() 
 join LiList in list.AsEnumerable().AsParallel()             
 on new
 {
     val = bogyconfiglist.LineItem.Trim().ToUpper(),
     val1 = bogyconfiglist.Section.Trim().ToUpper()
 }
 equals new
 {
     val = LiList.LI.Trim().ToUpper(),
     val1 = LiList.Section.Trim().ToUpper()
 }
 into conbogylist
 from confg in conbogylist.DefaultIfEmpty()
 select new QCHelper()
 {
     Section = bogyconfiglist.Section,
     Li = bogyconfiglist.LineItem,
     CrossCalc1Q = confg == null ? string.Empty : (confg.CrossCalc1Q == null ? "" : confg.CrossCalc1Q.Replace("~9999", string.Empty).Trim()),
     CrossCalc2Q = confg == null ? string.Empty : (confg.CrossCalc2Q == null ? "" : confg.CrossCalc2Q.Replace("~9999", string.Empty).Trim()),
     CrossCalc3Q = confg == null ? string.Empty : (confg.CrossCalc3Q == null ? "" : confg.CrossCalc3Q.Replace("~9999", string.Empty).Trim()),
     CrossCalc4Q = confg == null ? string.Empty : (confg.CrossCalc4Q == null ? "" : confg.CrossCalc4Q.Replace("~9999", string.Empty).Trim()),
     CrossCalcFY = confg == null ? string.Empty : (confg.CrossCalcFY == null ? "" : confg.CrossCalcFY.Replace("~9999", string.Empty).Trim()),
     AllowComma = confg == null ? false : confg.AllowComma,
     AllowedDecimalPlace = confg == null ? string.Empty : confg.AllowedDecimalPlace,
     AllowPercentageSign = confg == null ? false : confg.AllowPercentageSign,
     CurrencySign = confg == null ? string.Empty : confg.CurrencySign,
     IsQcCheck = confg == null ? false : confg.QCCheck,
     QcType = confg == null ? string.Empty : confg.QCType,
     FormulaLiConfig = confg == null ? string.Empty : (confg.StandrdFormula == null ? "" : confg.StandrdFormula.Replace("~9999", string.Empty).Trim()),
     xFundCode = bogyconfiglist.xFundCode == null ? string.Empty : bogyconfiglist.xFundCode
 }).Distinct().ToList<QCHelper>();