how to add records from DataTable to ArrayList using AddRange Method
Hey guys, i have a dataTable, which contains a column which has 100 rows, now i want to add those rows in ArrayList, but not with the Add Method, because i have to use for loop for adding records one by one, so i want to prefer addrange method of arraylist, so it is possible to add records in arraylist from开发者_运维知识库 DataTable using AddRange.
Below is the code which i am using.
Dim adapGetImages As New SqlDataAdapter("select distinct FileName from Files where Filename<>'' and (RIGHT(FileName,3) = 'gif' or RIGHT(FileName,3) = 'jpg' or RIGHT(FileName,3) = 'bmp') order by FileName", connection)
Dim dtGetImages As New DataTable()
adapGetImages.Fill(dtGetImages)
ArrayList1.AddRange(dtGetImages.Rows[0][0].ToString())
The last line is where i am stuck. as if i run this it will just add single row, and if i say dtGetImages.Rows.ToString()
then this will just Add System.DataRow in Arraylist and not its content.
Please reply what could be done for resolving this issue without any loops. Thanks in Advance.
Something like this should work:
ArrayList1.AddRange(dtGetImages.Rows.Cast(Of DataRow)().[Select](Function(r) r(0).ToString()).ToArray())
Update
There are a few concepts at work here. First of all, you're not trying to add the rows to the list, but rather the first value in each row. As you mentioned, you could use a for
loop to get this value out of each row and add it to a list, but loops like that aren't very concise or declarative. This is why we have LINQ (Language-Integrated Queries): a framework that simplifies the transformation of collections of values.
The Select
method is an extension method defined in the LINQ framework. It takes as an argument a function that defines what transformation to perform on each item in the given collection. When the result of the Select
is invoked, the given function is performed on each value, producing a new collection based on the results. Here we give it the function:
Function(r) r(0).ToString()
..., which tells it:
Given a variable
r
, get the zeroth index ofr
and callToString()
on the result.
So if this Select
method is called on an IEnumerable<DataRow>
, the resulting collection will be an IEnumerable<string>
. Since the ArrayList.AddRange
method requires an ICollection
, we have to change our IEnumerable<string>
into something that implements ICollection
. As it turns out, arrays implement ICollection
, so we can use the ToArray()
method from LINQ to convert it into a value that ArrayList.AddRange
will accept.
There's just one problem: the DataTable class was written before generics were part of the .NET framework, so the DataRowCollection
class implements the non-generic IEnumerable
interface rather than the strongly-typed IEnumerable<DataRow>
. Since our Select
method needs to start with an IEnumerable<DataRow>
, we have to do something to convert dtGetImages.Rows
into a IEnumerable<DataRow>
. The Cast
method, also provided by LINQ, will do this for us. We tell Cast
that we happen to know that everything in the given IEnumerable
(the DataRowCollection
in this case) is going to be a DataRow
, so it can safely cast each item into a DataRow
, leaving us with the IEnumerable<DataRow>
that we need to use the Select
method on.
Hopefully that explains things pretty well. One thing you should be aware of is that this doesn't eliminate the use of loops. When you call .ToArray()
, it pulls items from the .Select
, which pulls items from the .Cast
, which iterates over dtGetImages.Rows
the same as if you'd written a for
loop to populate an array. The only difference is that LINQ makes your code more declarative. It's the difference between saying:
For each DataRow in the Rows property of this DataTable, I'm going to create a variable called
row
. Then I will callToString
on the zeroth element of this variable and add the result to the ArrayList.
... and saying:
I am calling
ToString
on the zeroth element out of each DataRow in the Rows property of this DataTable, and adding the results to the ArrayList.
They both mean roughly the same thing, but one is both clearer and more concise.
The MSDN page for the Fill method has this note:
When handling batch SQL statements that return multiple results, the implementation of Fill and FillSchema for a .NET Framework data provider retrieves schema information for only the first result.
Are you getting the schema information for the first result?
精彩评论