score:70
well you could start from the list instead of the dictionary:
var selectedvalues = keystoselect.where(dictionary1.containskey)
.select(x => dictionary1[x])
.tolist();
if all the keys are guaranteed to be in the dictionary you can leave out the first where
:
var selectedvalues = keystoselect.select(x => dictionary1[x]).tolist();
note this solution is faster than iterating the dictionary, especially if the list of keys to select is small compared to the size of the dictionary, because dictionary.containskey
is much faster than list.contains
.
score:1
the accepted solution is still not the most efficient option from the point of lookups as you still have to check if the key is in the dictionary twice: once to filter the keys, once to lookup the object.
this solution does not have that issue:
var selectedvalues = keystoselect
.select(_ => {
var found = dict.trygetvalue(_, out tvalue? result);
return (found, result);
})
.where(_ => _.found)
.select(_ => _.result!)
.tolist();
score:3
if you know that all the value that you want to select are in the dictionary, you can loop through the keys instead of looping through the dictionary:
list<valuetype> selectedvalues = keystoselect.select(k => dictionary1[k]).tolist();
score:9
a dictionary<tkey,tvalue>
is ienumerable<keyvaluepair<tkey,tvalue>>
, so you can simply select
the value
property:
list<valuetype> selectedvalues = dictionary1
.where(x => keystoselect.contains(x.key))
.select(x => x.value)
.tolist();
or
var selectvalues = (from keyvaluepair in dictionary1
where keystoselect.contains(keyvaluepair.key)
select keyvaluepair.value).tolist()
Source: stackoverflow.com
Related Query
- How to select multiple values from a Dictionary using Linq as simple as possible
- How to select values within a provided index range from a List using LINQ
- How to select multiple values after using Max() in LINQ to Objects?
- How do I select multiple items from a database using LINQ
- How to read values from dictionary <String, List<Component>> on the basis of key passed using linq
- How to select multiple fields from a DataView and apply .Distinct() using LINQ
- How to populate a DropDownList from a SQL database using linq and only select Distinct Values
- Using linq to select possible values for multiple database columns in one SQL query (EF6)
- How to Select Multiple Fields from DataGridView using LINQ in C# WinForms
- how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#
- How to select from multiple tables using LINQ using a union query to return only one column?
- How to extract particular columns values from multiple rows using linq
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- How do I convert from a Dictionary to a SortedDictionary using LINQ in C#?
- Select distinct values from a list using LINQ in C#
- How do I remove items from generic list, based on multiple conditions and using linq
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- How to get Distinct Values from List(Of T) using Linq
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to consolidate results from multiple IEnumerable<T> using LINQ
- How can i select all values from a nested dictionary with linq?
- How to efficiently select certain items from two lists using LINQ
- How to select multiple columns from dataset into a string list with LinQ
- ASP.NET : select multiple values in query using Linq
- How to select multiple columns from datatable in linq group by?
- Using LINQ to select all from multiple tables
- How to iterate a list returned from a Dictionary using Linq and C#
- how to filter dictionary values if its key exists in a string[] using linq
- Select from multiple list using LINQ
- Return multiple rows from a select new using linq
More Query from same tag
- Enumerable.Zip alternative solution in Dot Net 3.0
- AddDays used in LINQ to Entities throwing error in all but one server
- how to update the multiple rows at a time using linq to sql?
- Create a Lambda Expression With 3 conditions
- Why Doesn't My Anonymous Method Work in a Loop?
- Casting and Linq Cast<T>()
- How do I programmatically translate a LINQ query to readable English text that correctly describes the linq expression?
- entity framework to a list<>?
- Need guidance on creating a new object that stores user information after comparing it
- How do I group/aggregate but return fields other than the aggregation field?
- .NET MongoDb - How can I insert an element to list in n-nested structure
- Finding bounds with LINQ
- why result of grouping join repeat 2 times?
- error adding value to list entity (LINQ C#) MVC 4
- Search SQL db using LINQ to SQL
- Do database schema changes break linq to entities
- Group dateTime by hour range
- Linq Dynamic ParseLambda not resolving
- Select one item from each category on the basis of cost
- EF LINQ Include with Take
- A LINQ Challenge
- Convert Hierarchical DataTable to Json
- Dynamically build LINQ filter for the Any() method?
- Is there a drawback in using rxjs for readonly collection manipulation
- how to perform sum on distinct different then the group field
- Linq Query Involving Association Tables
- Inserting & Updating Data in MonogoDB 3.4 with C# Driver 2.3 in an MVC application
- Dynamic OrderBy fails for nullable column
- LINQ - Dynamic Order By Expression
- using var, accessising contents outside scope, possible "cast by example??"