Reputation: 4718
I'm using the select method on a DataTable within a DataSet to return an array of DataRows. This works fine, but on my array I don't have a Count method??
I can't understand why, I've used almost the exact statement within another application and I do have the Count method.
I've checked the reference to System.Data in both apps and they are the same. I've also checked the using statement at the top of the class and they are both set the same. i.e. using System.Data;
Here is my code:
DataRow[] selectedRecords = myDataset.Tables["Records"].Select();
now I'm trying to do :
selectedRecords.Count()
but I have no Count Method!?!?!
I'm using C# 4.0
Thanks in advance.
Upvotes: 1
Views: 765
Reputation: 42497
Arrays use Length
. However, if you import the Linq namespace, there is a Count()
extension method.
Upvotes: 5
Reputation: 112344
While you can use the Length
property as others explained, an array also implements the IList
interface, which in turn implements ICollection
which defines a Count
property. However, it is implemented explicitly in arrays, which means that you can access it only through the interface
public int GetCountOf(ICollection coll) {
return coll.Count;
}
...
int cnt = GetCountOf(myArray);
Or
int cnt = ((ICollection)myArray).Count;
Count
here returns the length of the array and not the count of the items contained in the array, i.e. the array could have all null
entries.
Upvotes: 0
Reputation: 63338
.Count()
an array, you need a using System.Linq;
to be in force.Length
...Upvotes: 4
Reputation: 131334
Use Array.Length, not Count. Arrays do not have a Count property. If you are trying to use LINQ's Count() method specifically, you should import the System.Linq namespace
Upvotes: 5