How to get value from matching key from keyvalue pair list in vb.net using linq?












1















I want to have the value from keyvalue pair list which key matches the given data.
I have data in list as follows



LeftExistingLayerName
(0)=>{[data1, 0.04#0]}
(1)=>{[data2, 0.04#0]}
(2)=>{[data3, 0.04#0]}


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



Dim LeftExistingLayerName As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
Dim result As String()
If LeftExistingLayerName.Where(Function(x) x.Key.Contains(g.LayerName)).Any() Then
Dim result1 = LeftExistingLayerName _
.Where(Function(x) x.Key.Contains(g.LayerName)) _
.Select(Function(x) x.Value) _
.ToString()
result = result1.Split(New String() {"#"}, StringSplitOptions.None)
End If


I want to have the value in the string reslut1 and split "#" and take out the values 0.04 in result(0) and 0 in result1

I am getting this error




Conversion from string "System.Linq.Enumerable+WhereSele" to type 'Double' is not valid











share|improve this question


















  • 2





    The result of a LINQ query is an enumerable list. If you want one item from that list then use First, FirstOrDefault, Single or SingleOrDefault. Note that there is never a case where more than one of those methods is appropriate. They are for lists containing one or more items, zero or one or more items, one item and zero or one item respectively.

    – jmcilhinney
    Nov 22 '18 at 11:19
















1















I want to have the value from keyvalue pair list which key matches the given data.
I have data in list as follows



LeftExistingLayerName
(0)=>{[data1, 0.04#0]}
(1)=>{[data2, 0.04#0]}
(2)=>{[data3, 0.04#0]}


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



Dim LeftExistingLayerName As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
Dim result As String()
If LeftExistingLayerName.Where(Function(x) x.Key.Contains(g.LayerName)).Any() Then
Dim result1 = LeftExistingLayerName _
.Where(Function(x) x.Key.Contains(g.LayerName)) _
.Select(Function(x) x.Value) _
.ToString()
result = result1.Split(New String() {"#"}, StringSplitOptions.None)
End If


I want to have the value in the string reslut1 and split "#" and take out the values 0.04 in result(0) and 0 in result1

I am getting this error




Conversion from string "System.Linq.Enumerable+WhereSele" to type 'Double' is not valid











share|improve this question


















  • 2





    The result of a LINQ query is an enumerable list. If you want one item from that list then use First, FirstOrDefault, Single or SingleOrDefault. Note that there is never a case where more than one of those methods is appropriate. They are for lists containing one or more items, zero or one or more items, one item and zero or one item respectively.

    – jmcilhinney
    Nov 22 '18 at 11:19














1












1








1








I want to have the value from keyvalue pair list which key matches the given data.
I have data in list as follows



LeftExistingLayerName
(0)=>{[data1, 0.04#0]}
(1)=>{[data2, 0.04#0]}
(2)=>{[data3, 0.04#0]}


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



Dim LeftExistingLayerName As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
Dim result As String()
If LeftExistingLayerName.Where(Function(x) x.Key.Contains(g.LayerName)).Any() Then
Dim result1 = LeftExistingLayerName _
.Where(Function(x) x.Key.Contains(g.LayerName)) _
.Select(Function(x) x.Value) _
.ToString()
result = result1.Split(New String() {"#"}, StringSplitOptions.None)
End If


I want to have the value in the string reslut1 and split "#" and take out the values 0.04 in result(0) and 0 in result1

I am getting this error




Conversion from string "System.Linq.Enumerable+WhereSele" to type 'Double' is not valid











share|improve this question














I want to have the value from keyvalue pair list which key matches the given data.
I have data in list as follows



LeftExistingLayerName
(0)=>{[data1, 0.04#0]}
(1)=>{[data2, 0.04#0]}
(2)=>{[data3, 0.04#0]}


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



Dim LeftExistingLayerName As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
Dim result As String()
If LeftExistingLayerName.Where(Function(x) x.Key.Contains(g.LayerName)).Any() Then
Dim result1 = LeftExistingLayerName _
.Where(Function(x) x.Key.Contains(g.LayerName)) _
.Select(Function(x) x.Value) _
.ToString()
result = result1.Split(New String() {"#"}, StringSplitOptions.None)
End If


I want to have the value in the string reslut1 and split "#" and take out the values 0.04 in result(0) and 0 in result1

I am getting this error




Conversion from string "System.Linq.Enumerable+WhereSele" to type 'Double' is not valid








vb.net linq






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 9:56









ritesh khadkaritesh khadka

829




829








  • 2





    The result of a LINQ query is an enumerable list. If you want one item from that list then use First, FirstOrDefault, Single or SingleOrDefault. Note that there is never a case where more than one of those methods is appropriate. They are for lists containing one or more items, zero or one or more items, one item and zero or one item respectively.

    – jmcilhinney
    Nov 22 '18 at 11:19














  • 2





    The result of a LINQ query is an enumerable list. If you want one item from that list then use First, FirstOrDefault, Single or SingleOrDefault. Note that there is never a case where more than one of those methods is appropriate. They are for lists containing one or more items, zero or one or more items, one item and zero or one item respectively.

    – jmcilhinney
    Nov 22 '18 at 11:19








2




2





The result of a LINQ query is an enumerable list. If you want one item from that list then use First, FirstOrDefault, Single or SingleOrDefault. Note that there is never a case where more than one of those methods is appropriate. They are for lists containing one or more items, zero or one or more items, one item and zero or one item respectively.

– jmcilhinney
Nov 22 '18 at 11:19





The result of a LINQ query is an enumerable list. If you want one item from that list then use First, FirstOrDefault, Single or SingleOrDefault. Note that there is never a case where more than one of those methods is appropriate. They are for lists containing one or more items, zero or one or more items, one item and zero or one item respectively.

– jmcilhinney
Nov 22 '18 at 11:19












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer





















  • 1





    Thank you. That worked like a charm. Learned a lot from your answer

    – ritesh khadka
    Nov 23 '18 at 7:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428267%2fhow-to-get-value-from-matching-key-from-keyvalue-pair-list-in-vb-net-using-linq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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





share|improve this answer





















  • 1





    Thank you. That worked like a charm. Learned a lot from your answer

    – ritesh khadka
    Nov 23 '18 at 7:16
















1














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





share|improve this answer





















  • 1





    Thank you. That worked like a charm. Learned a lot from your answer

    – ritesh khadka
    Nov 23 '18 at 7:16














1












1








1







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





share|improve this 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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 19:32

























answered Nov 22 '18 at 19:17









AomineAomine

42.3k74473




42.3k74473








  • 1





    Thank you. That worked like a charm. Learned a lot from your answer

    – ritesh khadka
    Nov 23 '18 at 7:16














  • 1





    Thank you. That worked like a charm. Learned a lot from your answer

    – ritesh khadka
    Nov 23 '18 at 7:16








1




1





Thank you. That worked like a charm. Learned a lot from your answer

– ritesh khadka
Nov 23 '18 at 7:16





Thank you. That worked like a charm. Learned a lot from your answer

– ritesh khadka
Nov 23 '18 at 7:16




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428267%2fhow-to-get-value-from-matching-key-from-keyvalue-pair-list-in-vb-net-using-linq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules