ArrayList Manipulation?
it is possible to store data in of two arraylist
into <list>
?
here's my code with two arrays that will me开发者_C百科rge:
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);
You can do it with linq simply:
lstStockInfoPrice.AddRange(arr1.Cast<StockInfoPrice>());
lstStockInfoPrice.AddRange(arr2.Cast<StockInfoPrice>());
See Cast
in IEnumerable
.
It is possible.
You could try the following if oUtils.GetPrices(SymbolIndex) returns StockInfoPrice;
lstStockInfoPrice.AddRange(oUtils.GetPrices(SymbolIndex));
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
arrPrices = oUtils.GetPrices(SymbolIndex);
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:-
ArrayList arrPrices;
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util();
arrPrices = oUtils.GetPrices(SymbolIndex);
If you want to move the value from arrPrices
to lstStockInfoPrice
and lstStockInfoDetails
, you could iterate over the array list and put the elements in the list. Something like this:
foreach(var o in arrPrices)
{
lstStockInfoPrice.Add(o); // or Add((StockInfoPrice)o)
}
精彩评论