ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / Map Class / GetLayersAsFlattenedList Method
Example

In This Topic
    GetLayersAsFlattenedList Method (Map)
    In This Topic
    Returns a read only flat list of layers where nested groups are not preserved.
    Syntax
    Public Function GetLayersAsFlattenedList() As IReadOnlyList(Of Layer)
    public IReadOnlyList<Layer> GetLayersAsFlattenedList()

    Return Value

    A read only list of layers
    Example
    Get a list of layers filtered by layer type from a map
    {
        List<FeatureLayer> featureLayerList = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().ToList();
    }
    Get a layer of a certain geometry type
    {
        //Get an existing Layer. This layer has a symbol you want to use in a new layer.
        var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>()
                .Where(l => l.ShapeType == esriGeometryType.esriGeometryPoint).FirstOrDefault();
    }
    Find a layer
    {
        //Finds layers by name and returns a read only list of Layers
        IReadOnlyList<Layer> layers = map.FindLayers("cities", true);
    
        //Finds a layer using a URI.
        //The Layer URI you pass in helps you search for a specific layer in a map
        var lyrFindLayer = MapView.Active.Map.FindLayer("CIMPATH=map/u_s__states__generalized_.xml");
    
        //This returns a collection of layers of the "name" specified. You can use any Linq expression to query the collection.  
        var lyrExists = MapView.Active.Map.GetLayersAsFlattenedList()
                           .OfType<FeatureLayer>().Any(f => f.Name == "U.S. States (Generalized)");
    }
    Find a layer using partial name search
    {
        IEnumerable<Layer> matches = map.GetLayersAsFlattenedList().Where(l => l.Name.Contains("partialLayerName", StringComparison.CurrentCultureIgnoreCase));
        List<Layer> layers = new List<Layer>();
        foreach (Layer l in matches)
            layers.Add(l);
        System.Diagnostics.Debug.WriteLine($"Found {layers.Count} layers with name containing '{"partialLayerName"}'");
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also