score:14
it really does seem like you're overcomplicating this issue.
you can just use the indexer ([]
) of the dictionary
class along with the .containskey()
method.
if you use something like this:
string value;
if (mydict.containskey(key))
{
value = mydict[key];
}
else
{
console.writeline("key not present");
return;
}
you should achieve the effect that you want.
score:0
replace tostring();
with firstordefault();
when you are applying .where()
condition it will return an enumerable
. you have to either cast it to list using .tolist()
then you will get list of the values that meet the condition you used, or if you just want to get the first one you can use firstordefault();
you can also write it like this
listafirme.firstordefault(pair => pair.key == matchkey).value
since firstordefault()
accepts a predicate, you don't need always need to use .where
.
score:3
it seems that you've overcomplicated the usage. in this case you don't need linq
.
just use the dictionary
provided indexer: listafirme[matchkey]
. this returns the related value. if the key does not exist the dictionary
throws a keynotfoundexception
exception. if you want to check if the key exists you can use the containskey()
method which returns a bool
.
score:13
if you want to retrieve a value of a key from a dictionary access by indexer or trygetvalue
:
var value = listafirme[matchkey];
//if you don't know for sure that dictionary contains key
string value;
if(a.trygetvalue(matchkey, out value))
{
/* code here */
}
as for why you got the result you did: linq operations of where
and select
return an ienumerable<t>
so when doing tostring
on it it executes the tostring
of ienumerable
which is to print it's type.
notice that listafirme
isn't a good name for a dictionary
if you did not have a dictionary and wanted to return one item then you'd use firstordefault
:
var value = somelist.firstordefault(item => /* some predicate */)?.value;
Source: stackoverflow.com
Related Query
- Get single value from dictionary by key
- How to get nested key value pair from dictionary with linq
- How to get a specific dictionary key value from ListDictionary?
- Good way to get the key of the highest value of a Dictionary in C#
- c# dictionary get the key of the min value
- Get Value from key using linq
- How to get item from dictionary by value of property
- How to pass key value pairs from console app to a Dictionary
- get key value pairs from xml using linq
- Store value in dictionary or retrieve from concatened key
- Get keys for maximum value from dictionary
- How to get value from Dictionary to new Object in LINQ query
- How to get value from matching key from keyvalue pair list in vb.net using linq?
- Select Value from Dictionary if Key exists using LINQ
- How to select all Key Value Pairs from a dictionary for some keys
- Ho to get Key and Value in LINQ from Score percentile
- Linq - Get all keys from dictionary where all values in the value list are equal to criteria
- get selected value from combobox with data source is anonymous type
- How to get the value by a key from a super nested Json
- How to get a single value from a Dictionary<Key,Value> given a property that belongs to the type Value
- Get objects from dictionary with equal properties values for every Key
- How to get flattened list from dictionary key query where values are lists?
- Get specific value from GroupBy Key in foreach
- C# Dictionary of Dictionaries returns null value from select when matched on key reference
- LINQ queries possible to use Foreign key to get value from other table
- Get maximum value from Dictionary
- How to get value from IGrouping where key and value in object match
- Extract record from dictionary list where key and value does not contains given value list c#
- c# Linq or code to extract groups from a single list of source data
- How to get key value pair from appSettings into a collection of SelectListItem?
More Query from same tag
- Is there a good way to detect empty results in a Linq-To-Entities query?
- How to mock an IEnumerable<interface> type and pass it to constructor
- LINQ Statement for Generic List
- Get results of group by and add to multiple lists c#
- LINQ - Method vs Query Syntax Difference
- How to group by and order by, with LINQ, NHibernate and uNHAddins
- what LINQ query will return results based on partial matches against arbitrary number of an entity's fields?
- How can I use this extension method in a linq to entities query using query syntax?
- Passing value to the function
- how to update observable collection group
- Using Telerik ORM it is throwing error when joining 2 tables from different contexts
- Sorting a list in .Net by one field, then another
- how to read a specific xml element in windows 8 platform
- listbox troubles. I have no idea
- LinqDataSource dynamic parameters - forcing an OR with WhereParameters
- How to select different fields using .Concat for Union All query
- Linq - optimize/correct my query
- DataGridView listing selected item from ListBox
- Searching in ListArray C#
- Linq Query to Count Properties per value
- Query multiple databases with one query
- Nested group with LINQ
- LINQ Remove duplicates and result item to have sum of quantities
- How to Flatten Table or Concatenate Rows?
- Having trouble with a many to many linq select
- LINQ string contains another string case insensitive
- Protect connection string. LInq to sql
- How to select List in a List by Key through LINQ
- "Attach or Add an entity that is not new...loaded from another DataContext. This is not supported." Problem on insert of linker object
- Combining two Linq Expressions