score:2
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
Source: stackoverflow.com
Related Articles
- How to get value from matching key from keyvalue pair list in vb.net using linq?
- Get indexes of all matching values from list using Linq
- Simplest way to filter value from generic List in C# using LINQ
- Get index of matching value in List using LINQ
- Get groups of 4 elements from name value list using LINQ in C#
- Get minimum and maximum time value from list of object property using Linq
- Select a matching string randomly from a list using LINQ
- Linq Select: Using a value from a list if available
- Get the objects with the maximum value for a specific property from a list using LINQ
- C# JSON.Net parse and get list of all elements matching a value using LINQ
- Can't add a new record with an integer value into database by using linq from code C#
- Update List from another List with Matching values using Linq
- get new list which contain any numeric value from current list using LINQ query
- Filter one list using index value obtained from another list using linq
- How to filter a list by comparing a value from another list using Linq in C#?
- Using a Linq query to select objects where any value of a property matches values from a list
- How to fetch value against list of strings from xml using linq and xpath?
- Using LINQ to read key value pair from configuration file
- Using Linq when getting value from nested list
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- How to get multiple column value from database into a list using LINQ query
- Executing a linq query similar to Dictionary format using groupby to retrieve value from another list
- linq getting key from list keyvalue pair
- Get array of double from list of array of double, where first value in array is the max first value using linq
- c# Linq or code to extract groups from a single list of source data
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Extract key and value from a List of a List of objects using LINQ
- Searching if value exists in a list of objects using Linq
- How to select values within a provided index range from a List using LINQ
- compare two list and return not matching items using linq
- Simple update with Entity Framework
- Filtering a nested list and out put as tree
- Object from comma separated string using linq
- Filtering Dataset based on similar column values
- Value is in enum list
- how can i get the right fields into a group and sum using linq?
- Sort a C# list by word
- Doing a LINQ join, then group by, then a sum on two different columns
- list of lists of data structure - how do i access with linq?
- dynamic sort for linq expression function
- Match Characters in LINQ Query
- Read LINQ results
- LINQ Lambda, need to query to a very confuse database, how should I do it?
- Sorting OrderedEnumerableRowCollection .. strings like 00-000, 01-000 are not sorted as expected
- How to create lambda expression that returns object's property, having this property's name?
- Extract keys from Dictionary where all the values in the value are equal to searched value
- LINQ-to-sql + C#. Only retrieve some columns from table to gridview
- Linq to Xml Convert a list
- Injecting Code Into Linq
- Mocking an Anonymous type parameter in Dapper and Filtering result by parameter's property