score:136
are you trying to remove a single value or all matching values?
if you are trying to remove a single value, how do you define the value you wish to remove?
the reason you don't get a key back when querying on values is because the dictionary could contain multiple keys paired with the specified value.
if you wish to remove all matching instances of the same value, you can do this:
foreach(var item in dic.where(kvp => kvp.value == value).tolist())
{
dic.remove(item.key);
}
and if you wish to remove the first matching instance, you can query to find the first item and just remove that:
var item = dic.first(kvp => kvp.value == value);
dic.remove(item.key);
note: the tolist()
call is necessary to copy the values to a new collection. if the call is not made, the loop will be modifying the collection it is iterating over, causing an exception to be thrown on the next attempt to iterate after the first value is removed.
score:-2
in my case i use this
var key=dict.firstordefault(m => m.value == s).key;
dict.remove(key);
score:0
you can use the following as extension method
public static void removebyvalue<t,t1>(this dictionary<t,t1> src , t1 value)
{
foreach (var item in src.where(kvp => kvp.value.equals( value)).tolist())
{
src.remove(item.key);
}
}
score:1
here is a method you can use:
public static void removeallbyvalue<k, v>(this dictionary<k, v> dictionary, v value)
{
foreach (var key in dictionary.where(
kvp => equalitycomparer<v>.default.equals(kvp.value, value)).
select(x => x.key).toarray())
dictionary.remove(key);
}
score:2
loop through the dictionary to find the index and then remove it.
score:9
dictionary<string, string> source
//
//functional programming - do not modify state - only create new state
dictionary<string, string> result = source
.where(kvp => string.compare(kvp.value, "two", true) != 0)
.todictionary(kvp => kvp.key, kvp => kvp.value)
//
// or you could modify state
list<string> keys = source
.where(kvp => string.compare(kvp.value, "two", true) == 0)
.select(kvp => kvp.key)
.tolist();
foreach(string thekey in keys)
{
source.remove(thekey);
}
Source: stackoverflow.com
Related Query
- Remove Item in Dictionary based on Value
- how to remove half of the dictionary items based on value (DateTime) with LINQ in C#?
- Remove item from list based on condition
- Convert an array to dictionary with value as index of the item and key as the item itself
- linq remove item from object array where property equals value
- How to get item from dictionary by value of property
- Remove Duplicate item from list based on condition
- LINQ Sub-select from Dictionary based on value type
- how to change the value of dictionary using linq based on key?
- Efficient way to remove all entries in a dictionary lower than a specified value
- Using LINQ, find the minimum value of an item property, in a collection, in a dictionary
- LINQ to remove duplicate rows from a datatable based on the value of a specific row
- Is there a way to add or remove a line of an LINQ to DB Query based on if the value being checked is null?
- Find an item from collection based on the minimum value of a property
- Search the list and take item based on the index value of the match
- check if string contains dictionary Key -> remove key and add value
- Remove multiple keys with same value data from dictionary in C# using Linq
- Searching for an item in a list that's nested inside another list based on a property value in C# using LINQ?
- Remove duplicate items from list if certain properties match and get the top item based on some ordering
- Remove Element from xDocument based on value in repeating segment
- C# How do I add the Value of a Dictionary to a variable based on its Key?
- Get the index of item in list based on value
- Remove same value in Lists within a Dictionary in C# - LINQ
- Remove whole group if value of grouped item is zero
- How remove duplicate item by a property value in a List<>
- How to remove duplicates based on one equal value and one that is different using Linq?
- What's The Best Way To Remove Duplicate Rows Based On A Row Item In A DataTable With VB.Net?
- Place item distinct value and count in Dictionary
- LINQ - Remove elements based on value
- Entity Framework Code First Select Item Based on Relationship
More Query from same tag
- Can I update the UI from a LINQ binding?
- Entity Framework with LINQ query
- switch between aggregate functions with linq
- Questions regarding How do i tutorial (WPF/Entity Framework/ObservableCollections)
- linq: how to check if the value of one attribute exists
- Efficient foreach child evaluation in parallel
- Join the result of a left join to another table
- Linq: Select Most Recent Record of Each Group
- C# populate a hierarchy tree from bottom to top
- Linq AND searches
- LINQ query to flatten master/detail with sub-details
- How do you sort an XDocument parent node based its child?
- How To Know If A Certain Form Is Open?
- Index out of range. Can't get selectedIndexChanged as index for query
- How to read XML text (not attributes) into float?
- Group by clause vs. Distinct()
- Linq query returning null when trying to pass a column value from list object
- Data not getting displayed in jqgrid
- How to make two Joins generate one select?
- C# LINQ sum array properties
- How to access grouped values returned by a linq query
- Remove duplicates while merging lists using Union in LINQ
- How to cast a list of a base type to list of the derived type
- Decimal Precision Lost when saved to DB, C#. I am using Entity Framework
- Group list by multiple conditions, LINQ
- Include / ThenInclude with where in EF Core
- Add Object to Dictionary value in LINQ?
- Passing value from one model to another in a ViewModel
- Getting more then one value in the same time from Dictionary
- Using LINQ on SQL Server Table with Substrings, Loop Searching in ASP.NET