简体   繁体   English

System.Array' 不包含 'Count' 的定义

[英]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在我的 C# 项目中,我收到此错误,即计数的定义不包含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();
}

You don't need Count , instead you can use Length with arrays to get the count.您不需要Count ,而是可以将Length与数组一起使用来获取计数。

You are getting the error since you are missing using System.Linq;您收到错误,因为您缺少using System.Linq;

To get TotalImages you can use要获得TotalImages您可以使用

int TotalImages = fileInfo.Length;

While Count() isn't "needed" since the type is an Array, the error is a result of not "using" the correct namespaces.虽然Count()不是“需要”的,因为类型是一个数组,但错误是没有“使用”正确的命名空间的结果。

Enumerable.Count() is a LINQ extension method in the System.Linq namespace. Enumerable.Count()System.Linq命名空间中的 LINQ扩展方法。 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.由于数组实现IEnumerable<T> ,因此它们可以在可用时使用任何此类方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Array不包含“拆分”的定义 - System.Array does not contain a definition for 'Split' System.Array不包含ToArray的定义 - System.Array does not contain a definition for ToArray System.Array不包含添加的定义 - System.Array does not contain definition for Add System.Array 不包含“Split”的定义并且没有扩展方法“Split” - System.Array does not contain a definition for 'Split' And no extension method "Split" 我收到一个错误“ System.Array不包含LastWriteTime的定义” - i get an error “System.Array does not contain a definition for LastWriteTime” system.array不包含ToArray的定义,也没有扩展方法 - system.array does not contain a definition for ToArray and no extension method 名称冲突? &#39;System.Array&#39;不包含&#39;item&#39;的定义 - Name Conflict? 'System.Array' does not contain a definition for 'item' 错误95&#39;System.Array&#39;不包含&#39;FindIndex&#39;的定义 - Error 95 'System.Array' does not contain a definition for 'FindIndex' System.Array不包含“任何”的定义-C# - System.Array does not contain a definition for 'Any' - C# 为什么我用这个WinRT代码得到&#39;&#39;System.Array&#39;不包含&#39;AsBuffer&#39;的定义? - Why am I getting “'System.Array' does not contain a definition for 'AsBuffer'” with this WinRT code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM