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 Query
- How to get value from matching key from keyvalue pair list in vb.net using linq?
- How to get multiple column value from database into a list using LINQ query
- How to get nested key value pair from dictionary with linq
- How to get both key and value from list separately with linq query
- Get indexes of all matching values from list using Linq
- Get Value from key 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 key value pairs from xml using linq
- How to Deal With Dapper return Key Value pair with Dynamic from LINQ
- Get minimum and maximum time value from list of object property using Linq
- How do I get the first value from this collection using Linq to Entities?
- 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
- How can I get the next appropriate value from a generic list using LINQ?
- How to get list of keys in a dictionary with group by value with using LinQ
- Using Linq to object, how can I get one field's value based on another in the same list
- Using Linq how do get a list of users and their last login from two tables
- How to get items from SQL, using linq to sql, where Date is from today to 30 days ahead and return as a list
- get new list which contain any numeric value from current list using LINQ query
- how do i convert Dictionary string string to array of key value pair of string and string using linq and c#
- How to get lists of X and Y from a list of points using a single linq query?
- How to filter a list by comparing a value from another list using Linq in C#?
- How to fetch value against list of strings from xml using linq and xpath?
- How to get List of Properities from System.Collections.IEnumerable using LINQ C#
- How to get a list from a linq to Entity Framework query using vb.net
- Using LINQ to read key value pair from configuration file
- How to get values from class list inside a class using linq
- How to get top 4 result from a list using linq c#
- How get max count with min key from list integer with linq written in one expression
More Query from same tag
- LINQ NHibernate, latest related entity
- Cant seem to convert int to a string within linq expression
- LINQ statement value to ForEach C# WPF MVVM
- How to get flattened list from dictionary key query where values are lists?
- How do i change my LINQ query to show the correct XML?
- How to write this SQL query as a LINQ statement in .NET Core (C#)?
- How can I know the index of a XML Tag
- Lambda Expression Example
- How to make LINQ execute a (SQL) LIKE range search
- Why I can't call a function inside a linq query?
- Select distinct data on query
- Update query on temporary table in Linq
- Linq Complex Query Search between Dates
- How to neatly query corresponding object array items?
- List<string[]> determine max length by Linq
- How to collapse list of bools into list of ints
- Get one field value from LINQ query
- Matching two data sets with C# and LinQ
- linq query how to take out spaces in strings
- Create a datatable on the basis of a column of another Datatable Groupby using linq?
- Linq statement return answers for questions in each section
- LINQ-to-Entities with 8-bit ASCII data
- LINQ- filer as required model and add a list to the select statement
- Using Linq to find a very specific pattern from Oracle
- parsing large textfile output to another textfile
- Linq Update Problem
- How to Group Data between DateTime using Linq
- Get names from Dictionary's value as Comma separated value string
- LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance
- Simple update with Entity Framework