[英]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.