简体   繁体   中英

How I could loop over an array of objects and add a property from each object to a variable using Linq?

I have an array that contains the several instances of the following struct

public struct IMAGE_BASE_RELOCATION
{
    public uint VirtualAddress;
    public uint SizeOfBlock;
}

I want to add the value of SizeOfBlock to a variable for every struct in the array

Currently, I am doing this like this

var count = 0;

foreach(var structure in theArray)
{
    count += (int) structure.SizeOfBlock;
}

I was wondering how I could use linq to do this

您可以通过这种方式做到。

var count = theArray.Sum(x=>x.SizeOfBlock);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM