Siddiq Baig
Siddiq Baig

Reputation: 586

System.Array' does not contain a definition for 'Count'

In my C# Project, I am getting this error that the definition for count does not contain System.Array

protected void Page_Prerender(object sender, EventArgs e)
{
    FileInfo[] fileInfo;
    string UpFolder = Server.MapPath("~/data/images/");
    DirectoryInfo dir = new DirectoryInfo(UpFolder);
    fileInfo = dir.GetFiles();
    // here i am getting error
    int TotalImages = fileInfo.Count();
    for (int count = 0; count < TotalImages; count++)
    {
        Image image = new Image();
        image.ImageUrl = "~/data/images/" + fileInfo[count].ToString();
        image.ID = count.ToString();
        DataListImage.Controls.Add(image);
    }
    DataListImage.DataSource = dir.GetFiles();
    DataListImage.DataBind();

    string UpFolder1 = Server.MapPath("~/data/thumbnails/");
    DirectoryInfo dir1 = new DirectoryInfo(UpFolder1);
    DataListthumbnails.DataSource = dir1.GetFiles();
    DataListthumbnails.DataBind();
}

Upvotes: 6

Views: 32248

Answers (2)

user2864740
user2864740

Reputation: 61875

While Count() isn't "needed" since the type is an Array, the error is a result of not "using" the correct namespaces.

Enumerable.Count() is a LINQ extension method in the System.Linq namespace. As such, it can be made available with the following:

using System.Linq;

Since arrays implement IEnumerable<T>, then they can utilize any such methods when they are made available.

Upvotes: 6

Habib
Habib

Reputation: 223247

You don't need Count, instead you can use Length with arrays to get the count.

You are getting the error since you are missing using System.Linq;

To get TotalImages you can use

int TotalImages = fileInfo.Length;

Upvotes: 25

Related Questions