score:46
var values = dictionary.where(x => somekeys.contains(x.key)).select(x => x.value);
var keys = dictionary.where(x => somevalues.contains(x.value)).select(x => x.key);
score:2
the best approach is to perform your linq query on the collection of key-value pairs, then use a select projection to select either the keys or the values at the end of your query. this way there is no need to perform a look-up at the end of your query.
for example:
dictionary<string, string> data = new dictionary<string, string>();
// select all values for keys that contain the letter 'a'
var values = data.where(pair => pair.key.contains("a"))
.select(pair => pair.value);
score:11
a dictionary<,>
really isn't great for finding keys by value. you could write a bidirectional dictionary, as i have done in this answer, but it wouldn't necessarily be the best approach.
of course you can use a dictionary as a sequence of key/value pairs, so you could have:
var keysforvalues = dictionary.where(pair => values.contains(pair.value))
.select(pair => pair.key);
just be aware this will be an o(n) operation, even if your "values" is a hashset
or something similar (with an efficient containment check).
edit: if you don't really need a key/value relation - if it's more like they're just pairs - then using list<tuple<foo, bar>>
would make a certain amount of sense. the query ends up being the same, basically:
public ienumerable<t1> getallfirst<t1, t2>(ienumerable<tuple<t1, t2>> source,
ienumerable<t2> seconds)
{
hashset<t2> secondsset = new hashset<t2>(seconds);
return source.where(pair => secondsset.contains(pair.item2));
}
public ienumerable<t2> getallsecond<t1, t2>(ienumerable<tuple<t1, t2>> source,
ienumerable<t1> firsts)
{
hashset<t1> firstsset = new hashset<t1>(firsts);
return source.where(pair => firstsset.contains(pair.item1));
}
Source: stackoverflow.com
Related Query
- LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
- Getting distinct and ordered members from a list of strings - linq or hashset for unique which one is faster / better suited
- Linq - Get all keys from dictionary where all values in the value list are equal to criteria
- Get keys (objects) from a dictionary that contains list and filtered by values
- Getting Sum of Multiple Columns and summing up values for the same id from a list
- Creating a dictionary with LINQ and keys from nested list
- LINQ for checking whether all members of a list are contained within values from a Dictionary
- Convert a list to a dictionary and sum up values using linq
- Search dictionary values and return List of keys meeting conditions
- How do I combine the keys and values of a Dictionary into one List using LINQ?
- Getting Keys and Values from IEnumerable<Dictionary<string, object>>
- Using Linq to filter out certain Keys from a Dictionary and return a new dictionary
- Get keys as List<> from Dictionary for certain values
- How to get keys and values from Appsettings of Config file in Linq
- List of objects to Dictionary with distinct keys and selected values
- LINQ: Given A List of Objects, Create a Dictionary with child objects as keys and Parent Objects as Value
- Linq query to Check and get dictionary item from list of dictionary items
- Linq to XML, select all attributes and their values for a given node
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- How to iterate a list returned from a Dictionary using Linq and C#
- Linq "group by" values in a Dictionary property with a list of keys
- Group and Merge multiple values from single List using LINQ
- What's a good algorithm for checking if a given list of values alternates up and down?
- Linq group by multiple columns and getting extra values from table before group by clause
- Using LINQ and EF, how to remove values from database where not in list of items
- How to extract values from a list of dictionaries and group and convert to a single dictionary with Linq?
- C# LINQ code for two list compare and replace
- C# Linq Group elements from list and count sum of values
- Getting the time difference using LINQ from a list of TimeSpans and parsing the time in as only minutes and seconds
- Compare keys of dictionary with values of List and return all the matching values including duplicates
More Query from same tag
- Group by year and month with given date range using Lambda or LINQ Query
- Removing duplicates from Array with C#.NET 4.0 LINQ?
- How to replace datetime in var list with date linq
- Use lambda/linq on structs
- Matching the order of one array to another
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- Make linq aware of uniqe ID
- How to create a list of Guid (Guid []) from a list of objects with linq?
- Lambda or LINQ for result
- Select Subset in Linq
- Foreach Accessing XML elements inside nodes
- Possible to update a member for all objects in a collection using LINQ?
- This code seems to return all rows
- WhereIn with dynamic property
- Pivoting a collection of arrays
- Accessing values of XML using Linq
- how to use Foreign key in L2E or EF when needs insert?
- Implementing Custom GroupBy function in VB.Net
- Using Dynamic Linq with late bound crm 2015 entities
- IEnumerable<T> result from a method and deferred execution
- Calling a stored procedure using LINQ to SQL with a Custom Entity Class
- C# Using Enumerable Range and Except with custom class to determine missing sequence number
- Linq join two lists: is it more efficient to use Dictionary?
- LINQ statement for joining multiple tables (Entity Framework 6)
- How to use LINQ query with DataGridView but a subset of table columns?
- Find rows where child collection contains all elements of the list
- LINQ - List within a List filtering
- Updating property of deserialised JSON object (without a defined deser type)
- Looping through multiple collection of arrays
- How return distinct records using LINQ