score:1
Accepted answer
You can use some thing like this :
Dictionary<string, int> dictionary = new Dictionary<string, int> {{"A", 1}, {"B", 2}, {"C", 3}};
List<string> list = new List<string> {"A","B"};
var result = dictionary.Where(x => list.Contains(x.Key)).ToList();
or
var result = dictionary.Where(x => list.Contains(x.Key)).ToDictionary(x=>x.Key,x=>x.Value);
score:1
There are multiple ways to achieve what you need
In case all keys of D1 are present in L1 without mutating D1
Dictionary<string, string> D2 = new Dictionary<string, string>();
L1.ForEach(x => D2[x] = D1[x]);
OR
var D2 = L1.ToDictionary(el => el, key => D1[key]);
Safe option:
var D2 = D1.Keys.Intersect(L1).ToDictionary(key => key, key => D1[key]);
and even more, depends on your creativity
Note that this is slow for big list and dictionary
D1.Where(x => L1.Contains(x.Key))
Source: stackoverflow.com
Related Articles
- Filter dictionary on the basis of key value present in list
- LINQ filter by type on list from dictionary value
- Filter a Dictionary by value where value is a list of string using LINQ
- LINQ - Convert List to Dictionary with Value as List
- Filter linq list on property value
- Simplest way to filter value from generic List in C# using LINQ
- Linq Query Dictionary where value in List
- Finding a value from within a List of Dictionary objects
- Use linq expression to filter a dictionary with a list of keys
- linq remove items from query where any list value is present
- How can I use drop down list value (String) to filter linq results?
- How to make a SUM of Dictionary Value nested into a list with LINQ?
- LINQ filter list based on property value and filter the properties as well
- LINQ: Given A List of Objects, Create a Dictionary with child objects as keys and Parent Objects as Value
- Return list with more than one value stored in a dictionary using linq
- How to add items in dictionary in which value is list of class?
- Return a value when the element is not present in list using linq
- C# Using LINQ to filter each Object with Max Value in List of Objects
- Filter a list based on value in an order?
- Filter a List using Dictionary keys
- How to find the key of a dictionary when the value is a list of objects in C#?
- How to filter list of dictionary values with criteria
- Find object in list with given property value then find dictionary value
- Search by zip code and filter list within specific radius?
- Find list value against certain key in Dictionary
- How to get list of keys in a dictionary with group by value with using LinQ
- How can you filter list by property value in an int list?
- c# Linq filter out records from a list within a dictionary
- C# Linq adding to dictionary with list as value
- Linq extension. Change property value in source list
- How to find duplicate items based on multiple values using LINQ?
- insert data in many-to-many relationship in Entity Framework
- OrderBy in EF linq to entities can't handle the goodness I'm throwing at it.
- Linq query where First() appears to return null when results are not found
- Using Linq and C#, is it possible to join two lists but with interleaving at each item?
- Query String from IQueryable built using PredicateBuilder
- Linq, should I join those two queries together?
- LINQ to SQL Queries odd Materialization
- Name does not exist in the current context but table is listed
- using int.TryParse with Queryable and expression
- Column doesn't populate with lazy loading turned off
- How can I populate tuple using LINQ?
- Tricky combination of two lists using Linq
- OrderBy in Calculated Columns in LINQ?
- find object from Tree or List Hierarchy
- NHibernate - Select Id from reference column without join
- How to only return specific object list base on the condition
- How to measure the time for LINQ Query?
- Adding a method call to a Linq Expression whilst remaining an full expression
- Sitecore Content Search, converting to List<item> and editing