First or Default

85
3
Jump to solution
06-12-2018 01:43 PM
Highlighted
Regular Contributor

Hi,

When using the following code to find a specific layer in my map, I am finding that if a similarly named layer exists first in the TOC, then the FeatureLayer object gets set to it.

FeatureLayer sanForceMainLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name.IndexOf("GISWRKS1.WORKS.ForceMain", StringComparison.CurrentCultureIgnoreCase) >= 0).FirstOrDefault();

For example, if I have a TOC like this:

layer1

layer2

GISWRKS1.WORKS.ForceMain_removals

layer4

layer5

GISWRKS1.WORKS.ForceMain

layer7

In this case, sanForceMainLayer is getting set to 'GISWRKS1.WORKS.ForceMain_removals'.  I am expecting it to match exactly to 'GISWRKS1.WORKS.ForceMain'.

Thanks,

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Contributor

Brian, just change your test from "IndexOf" to "==" or string.Compare. Currently, you are testing for any layer that contains the string "GISWRKS1.WORKS.ForceMain" which is not what you want.

p.s. As an alternative, you can also include lambdas in most all LINQ extensions if you want to make your statements more concise...[for example Where(x => x == 1).FirstOrDefault() versus FirstOrDefault(x => x == 1)]

https://stackoverflow.com/questions/8059285/c-sharp-linq-whereexpression-firstordefault-vs-firstorde...

View solution in original post

Reply
0 Kudos
3 Replies
Highlighted
Esri Contributor

Brian, just change your test from "IndexOf" to "==" or string.Compare. Currently, you are testing for any layer that contains the string "GISWRKS1.WORKS.ForceMain" which is not what you want.

p.s. As an alternative, you can also include lambdas in most all LINQ extensions if you want to make your statements more concise...[for example Where(x => x == 1).FirstOrDefault() versus FirstOrDefault(x => x == 1)]

https://stackoverflow.com/questions/8059285/c-sharp-linq-whereexpression-firstordefault-vs-firstorde...

View solution in original post

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Hey Brian,

Working as intended?

IndexOf for strings reports the occurrence of string in the current string object.

If you had said IndexOf("a") it would have returned Layer1 for example.

You need to check if one string equals another so you could use either:

      var lyr = MapView.Active.Map.GetLayersAsFlattenedList().Where(l => l.Name.ToLower() == "streams").FirstOrDefault();

      var lyr2 = MapView.Active.Map.FindLayers("streams").FirstOrDefault();

Include .OfType<FeatureLayer> as needed.

You asked a related question last year: Does a certain layer exist in map

Sean

Highlighted
Regular Contributor

Thanks Charles Macleod‌ and Sean Jones

As I'm really just a part-time coder I had no idea that this was connected to LINQ, so I wasn't really sure where to start looking for answers.  I will definitely investigate LINQ now that I know.

I've really just been using the method Sean talks about when he mentions my 'related question', but now that I know how to structure the command and what the l => l == l is all about, I can definitely make some changes to how I do things.

This is what I ended up with in the end:

FeatureLayer sanForceMainLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.ForceMain").FirstOrDefault();

Thanks!!

Reply
0 Kudos