Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Value from key using linq

I have Dictionary from string key i want to get Value of corresponding key using Linq

like image 725
PramodChoudhari Avatar asked Feb 03 '11 13:02

PramodChoudhari


2 Answers

Why do you want to get a value from a Dictionary using LINQ? You can just get the value using:

int value = dictionary[key];

You could use Single, but it's totally pointless and more code:

var keyValuePair = dictionary.Single(x => x.Key == key);
int value = keyValuePair.Value;
like image 95
djdd87 Avatar answered Oct 09 '22 00:10

djdd87


Why use Linq for something that is built in?

var val = myDict[key];

Use Linq where it makes sense (querying collections), not for something that is already well handled by the Dictionary classes.

like image 35
Oded Avatar answered Oct 09 '22 01:10

Oded