score:2

Accepted answer

You're not showing the code that is causing the problem as the error states there was an issue when "attempting to convert a string to a double". Nothing in your code illustrates this.

Anyhow, to the point...

The string "System.Linq.Enumerable+WhereSele" is clearly not what you want to convert to a double value. The string representation comes from the fact that you're invoking ToString() on an Enumerable returned from the Select clause then trying to convert that to a double.

Given you've said that:

I have used following code to get the single value e.g matching data1

We can agree that you're expecting a single value back from the Enumerable query, As @jmcilhinney has suggested in the comments there are various methods that do this for you each of which are made for a specific scenario.

i.e.

  • First
  • FirstOrDefault
  • Single
  • SingleOrDefault

I'll let you investigate the little details that differentiate them, but for now and for example purposes you can use FirstOrDefault.

So, your code becomes:

Dim result As String = LeftExistingLayerName _                                                             
              .Where(Function(x) x.Key.Contains(g.LayerName)) _                                               
              .Select(Function(x) x.Value) _
              .FirstOrDefault()

reads as "return the first value of the KeyValuePair where the KeyValuePair's key contains g.LayerName otherwise the default value of a reference type"

So, at this point, we can then split the string by the delimiter "#" and covert the first, second items of the array to a double or any other type.

Dim array As String() = LeftExistingLayerName _                                                             
                  .Where(Function(x) x.Key.Contains(g.LayerName)) _                                               
                  .Select(Function(x) x.Value) _
                  .FirstOrDefault() _
                  .Split(New String() {"#"}, StringSplitOptions.None)
   ' array(0) gets the 0.04 part of your example
   ' array(1) gets the 0 part of your example

This should suffice as long as the predicate Function(x) x.Key.Contains(s) is always guaranteed to be met. Otherwise, you'll get a NullReferenceException upon invoking .Split on a null reference returned by FirstOrDefault.

If you want to handle that scenario then you can use the null propagation operator (?).

Dim array As String() = LeftExistingLayerName _                                                             
                      .Where(Function(x) x.Key.Contains(g.LayerName)) _                                               
                      .Select(Function(x) x.Value) _
                      .FirstOrDefault() _
                      ?.Split(New String() {"#"}, StringSplitOptions.None)

Btw, you can simplify:

If LeftExistingLayerName.Where(Function(x) x.Key.Contains(g.LayerName)).Any() Then

to:

If LeftExistingLayerName.Any(Function(x) x.Key.Contains(g.LayerName)) Then

Related Query

More Query from same tag