Summary of Metadata of items - Sum of ItemXYZ.Value #7667
Answered
by
jrdodds
MeikTranel
asked this question in
Q&A
-
Is there any way to resolve the sum of all <ItemGroup>
<ItemXYZ Include="A" Value="5" />
<ItemXYZ Include="B" Value="2" />
<ItemXYZ Include="C" Value="3" />
</ItemGroup> |
Beta Was this translation helpful? Give feedback.
Answered by
jrdodds
Jun 3, 2022
Replies: 1 comment 3 replies
-
Are you asking how to sum the numbers, i.e. 5 + 2 + 3 = 10? Here is a solution that uses an inline task. <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ItemXYZ Include="A" Value="5" />
<ItemXYZ Include="B" Value="2" />
<ItemXYZ Include="C">
<Value>3</Value>
</ItemXYZ>
</ItemGroup>
<Target Name="Main">
<Sum Addends="@(ItemXYZ->Metadata('Value'))">
<Output TaskParameter="Total" PropertyName="Sum" />
</Sum>
<Message Text="Sum = $(Sum)" />
</Target>
<UsingTask TaskName="Sum" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Addends ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<Total ParameterType="System.Int32" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
Total = 0;
foreach (var addend in Addends)
{
if (int.TryParse(addend.ItemSpec, out int result))
{
Total += result;
}
}
]]>
</Code>
</Task>
</UsingTask>
</Project> The The |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
MeikTranel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are you asking how to sum the numbers, i.e. 5 + 2 + 3 = 10?
Here is a solution that uses an inline task.