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
- Exclude all in Entity Framework (lazy loading enabled)
- C# use Linq method instead of for loop
- LINQ: Call Stored Procedure and Join its results with IQueryable
- Linq Nested Inner Joins
- Is there a way to transform a list of key/value pairs into a data transfer object
- ASP.net Linq to SQL grouping and adding at the same time?
- Massive SQL query requires conversion to LINQ
- List<MyObject> Contains
- Filter a generic list based on a reflected value of its entries with linq
- Simplifying complex linq query with multiple conditions
- I want to sort the string array. I know to achieve the result in diff ways. But I want to know why this method is throwing an error
- LINQ join on a subquery with a union
- How to reset the identity column in C#?
- Sql query to Linq query where condition
- How to use Linq to group, sum or retrieve total. My current code returns nothing
- Get index of first list and use it in another list
- LINQ Groupby query on a single column
- List<IEnumerator>.All(e => e.MoveNext()) doesn't move my enumerators on
- Select distinct from dataTable.AsEnumerable
- Filter a list<object> by another list<object2> C# LINQ
- IQueryable does not contain a definition for 'Select'
- Reuse index transformer expressions fails on Id
- Typescript: Equivalent of linq .GroupBy(key => key.IdItem).Select(item => item.Single());
- Get count of unique characters between first and last letter
- Setting default for DefaultIfEmpty method in LINQ when the default has multiple with-many relationships?
- How to Return Extra Data with IQueryable method?
- Logical difference between LINQ Where() and FirstOrDefault()
- Clean up data into a DataTable by Creating separate Groups using DataTable
- C# Combine Two Lists With Overlapping Data
- Retrieving matched items from an .Any() query