Click here to Skip to main content
15,943,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a List<featurelayer> in which all the layers are, and I also have a List<string> where I have the names of the layers that I want to keep in the previous list. How can I remove it? I tried it through the index but without success. List<string> seznamJmen is a list of the real names of the layers in List<featurelayer> vsechnyVrstvy.

What I have tried:

C#
 var seznamJmen = new List<string>();
List<FeatureLayer> vsechnyVrstvy = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().ToList();
 foreach (var jmena in seznamJmen)
                {
                    var index = vsechnyVrstvy.FindIndex(x => x.Name(jmena));
                }


Thanks for the advice and tips.

David
Posted
Updated 4-Oct-22 21:29pm

You can't remove anything from a collection if you are iterating through that collection in a foreach - that will automatically throw an exception.

Instead, in the foreach build a new "deletables" collection, and remove them once the foreach is finished - either with a second foreach, or using the Linq Except method:
C#
myList = myList.Except(deletables).ToList();
 
Share this answer
 
Comments
dejf111 5-Oct-22 3:08am    
Thank you so much!
OriginalGriff 5-Oct-22 3:42am    
You're welcome!

I think you can achieve what you need with one line of Linq. Something like this.

C#
List<(string name, int id)> names = new() { ("A", 0), ("B", 1),
                                                 ("C", 2), ("D", 3), ("E", 4) };
List<string> ids = new() { "B", "D", "A" };
var filteredList = names.Where(n=>ids.Contains(n.name)).ToList();

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900