简体   繁体   English

如何使用Linq遍历对象数组并将每个对象的属性添加到变量?

[英]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 我想将SizeOfBlock的值添加到数组中每个结构的变量中

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 我想知道如何使用linq做到这一点

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

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

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

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