FirstOrDefault not found in ArcGIS Pro SDK for Project.Current.GetItems

1025
2
Jump to solution
05-27-2021 10:12 AM
RichardDaniels
Occasional Contributor III

Hello,

Liberally sprinkled through the code snippets provided in the ArcGIS Pro SDK there are several references to using:

.FirstOrDefault

Any idea on how to get it to work?

I'm attempting the following:

MapProjectItem mpi = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(m => m.Name.Equals(strMapName, StringComparison.CurrentCultureIgnoreCase));

I'm using the above to retrieve the first occurrence of  a map element in a project with the given name in an array that meets the search criteria. However,  the above method is not part of the GetItems class. As a result Visual Studio returns "IEnumeratable<MapProjectItem> does not contain a definition for .FirstOrDefault and no accessible extension accepting a first argument could be found."

0 Kudos
1 Solution

Accepted Solutions
KrisCulin
Occasional Contributor

Make sure you add a using statement for System.Linq.  FirstOrDefault() is actually an extension method defined in that namespace.

Kris

View solution in original post

0 Kudos
2 Replies
KrisCulin
Occasional Contributor

Make sure you add a using statement for System.Linq.  FirstOrDefault() is actually an extension method defined in that namespace.

Kris

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

GetItems returns a type of 

 IEnumerable<MapProjectItem>

As Kris mentioned above if you include System.Linq you will get a set of extension methods for any object that implements the IEnumerable  interface (and other interfaces and classes as well) as for example:  Enumerable.FirstOrDefault Method (System.Linq) | Microsoft Docs  

To understand LINQ (classes and interfaces that support queries that use Language-Integrated Query (LINQ)) better you can search for Microsoft for help on LINQ.

0 Kudos