My code has something like this:
HttpFileCollection files
Instead of looping through each file and adding up the file.ContentLength to get the total length of all content e.g.
int totalLength = 0;
for (int i = 0; i < files.Count; i++)
{
totalLength += files[i].ContentLength;
}
Is there a way I can do this with a lambda expression so I have something like..
int totalLength = files.[some sort of delegate here to do the addition].
Thanks in advance.
Edit: HttpFileCollection has a GetEnumeratorMethod but would it need to implement IEnumerable to use a lambda expression?
You could use LINQ:
Unfortunately, HttpFileCollection's enumerator returns an enumeration of strings. In order to get the actual objects (HttpPostedFile), you need to access the "dictionary" by key. This converts the enumerator to an enumeration of HttpPostedFile instances (via Select), then sums the content length.