数组列表操作?
是否可以将两个arraylist
中的数据存储到
中?
这是我的代码,其中包含两个将合并的数组:
ArrayList arrPrices = new ArrayList();
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util();
arrPrices = oUtils.GetPrices(SymbolIndex);
ArrayList arrDetails = new ArrayList();
List<StockInfoDetails> lstStockInfoDetails = new List<StockInfoDetails>();
Util oUtils = new Util();
arrPrices = oUtils.GetDetails(SymbolIndex);
it is possible to store data in of two arraylist
into <list>
?
here's my code with two arrays that will merge:
ArrayList arrPrices = new ArrayList();
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util();
arrPrices = oUtils.GetPrices(SymbolIndex);
ArrayList arrDetails = new ArrayList();
List<StockInfoDetails> lstStockInfoDetails = new List<StockInfoDetails>();
Util oUtils = new Util();
arrPrices = oUtils.GetDetails(SymbolIndex);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 linq 简单地完成此操作:
请参阅
Cast
在IEnumerable
中。You can do it with linq simply:
See
Cast
inIEnumerable
.这是可能的。
如果 oUtils.GetPrices(SymbolIndex) 返回 StockInfoPrice; 您可以尝试以下操作;
It is possible.
You could try the following if oUtils.GetPrices(SymbolIndex) returns StockInfoPrice;
如果这个 Util 类不是你自己的,那么你就只能接受 Marius 的答案了。但是,如果您控制该 Util 类,则可以使 GetPrices 和 GetDetails 方法分别返回类型为 IEnumerable 和 IEnumerable 的内容。
然后,您可以使用 List.AddRange() 方法将整个批次添加到另一个列表中。
顺便说一句,您在 arrPrices 声明中的分配是浪费时间 - 分配的对象永远不会被使用,然后将被垃圾回收。
您的 GetPrices() 方法返回一个 ArrayList - 即一个新的 arrayList,并
简单地使 arrPrices 引用新列表。然后,就没有对您在声明 arrPrices 时分配的引用了,因此它被丢弃了。
这样做:-
I this Util class isn't your own, then you're stuck with Marius' answer. However, if you control that Util class then you could make the GetPrices and GetDetails methods return someting with type IEnumerable and IEnumerable respectively.
Then, you can add the whole lot to another list with List.AddRange() method.
As an aside, your allocation in the declaration of arrPrices is a waste of time - the allocated object is never used and will then be subject to garbage collection.
Your GetPrices() method returns an ArrayList - ie, a new arrayList, and
simply makes arrPrices refer to the new list. There are then no references to the one you allocated when you declared arrPrices, so it's thrown away.
Do it like this:-
如果您想将值从
arrPrices
移动到lstStockInfoPrice
和lstStockInfoDetails
,您可以迭代数组列表并将元素放入列表中。像这样的事情:If you want to move the value from
arrPrices
tolstStockInfoPrice
andlstStockInfoDetails
, you could iterate over the array list and put the elements in the list. Something like this: