# How to select first item in an ItemGroup

I was entirely unaware of this [simple trick](https://gist.github.com/shadow-cs/cb5499b010bdacd1f778be29daf7f04c)! Turns out that when you assign a property value to an item metadata, and there are multiple items, all items are iterated and the property is assigned consecutively to each item metadata, leaving you with the last such item metadata as the property value.

If you want the first item, you reverse the item group using the built-in item function and then assign the property:

```markup
<Target Name="FirstCompile" BeforeTargets="Compile">
  <ItemGroup>
    <Reversed Include="@(Compile->Reverse())" />
  </ItemGroup>
  <PropertyGroup>
    <First>%(Reversed.Identity)</First>
  </PropertyGroup>

  <Message Text="First compile item is $(First)" Importance="high" />
</Target>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://til.cazzulino.com/msbuild/how-to-select-first-item-in-an-itemgroup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
