score:13
if you mean only the values that occur more than once, you can do group the key/value pairs by value, filter out the groups with only one item and select the common group key (original value that appears multiple times) from what remains:
var multiples = dictionary.groupby(p => p.value)
.where(g => g.count() > 1)
.select(g => g.key);
if you want all pairs of keys/values where the values occur more than once, this variation will do it:
var multiples = dictionary.groupby(p => p.value)
.where(g => g.count() > 1)
.selectmany(g => g);
the only difference is in the last step, where after casting out groups with just one value the contents of all remaining groups are "unwrapped" into a single sequence of key/value pairs.
in the latter case, you can turn the results back into a dictionary (essentially filtering out values that appear only once) by following up with
.todictionary(p => p.key, p => p.value)
the last example, in query form:
var multiples = from pair in dictionary
group pair by pair.value into grp
where grp.count() > 1
from pair in grp select pair;
score:0
dictionary<int,string> d = new dictionary<int,string>();
d.add(1, "entries");
d.add(2, "images");
d.add(3, "views");
d.add(4, "images");
d.add(5, "results");
d.add(6, "images");
d.add(7, "entries");
d.groupby(x => x.value)
.where(x=>x.count()>1)
.selectmany(x => x)
.todictionary
(
x => x.key,
x=>x.value
);
score:0
with these extension methods you have a (little bit) faster and general solution (works on all ienumarable sources):
public static class extensions
{
public static ienumerable<tsource> duplicates<tsource, tkey>(this ienumerable<tsource> source, func<tsource, tkey> selector)
{
var grouped = source.groupby(selector);
var morethen1 = grouped.where(i => i.ismultiple());
return morethen1.selectmany(i => i);
}
public static bool ismultiple<t>(this ienumerable<t> source)
{
var enumerator = source.getenumerator();
return enumerator.movenext() && enumerator.movenext();
}
}
fill the source:
// create the source
var dictionary = new dictionary<int, string>
{
{1, "entries"},
{2, "images"},
{3, "views"},
{4, "images"},
{5, "results"},
{6, "images"},
{7, "entries"}
};
here are 3 options (each option is improved):
// more or less the same solution as other answers
var multiples1 = dictionary.groupby(p => p.value)
.where(g => g.count() > 1)
.selectmany(g => g);
// a little bit faster because ismultiple does not enumerate all values
// (count() iterates to the end, which is not needed in this case).
var multiples2 = dictionary.groupby(p => p.value)
.where(g => g.ismultiple())
.selectmany(g => g);
// easy to read
var multiples3 = dictionary.duplicates(p => p.value);
score:2
var res = dict.groupby(x => x.value).where(y => y.count() > 1);
if you want to get a dictionary:
var res = dict.groupby(x => x.value)
.where(y => y.count() > 1)
.selectmany(z => z)
.todictionary(k => k.key, k => k.value);
Source: stackoverflow.com
Related Query
- Occurance of Values in a Dictionary C#
- How to select multiple values from a Dictionary using Linq as simple as possible
- Convert dictionary values to list using linq
- How to calculate the sum of all values in a dictionary excluding the first item's value?
- LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
- Count values in Dictionary using LINQ and LINQ extensions
- Convert a list to a dictionary and sum up values using linq
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- Search dictionary values and return List of keys meeting conditions
- How to find key value pair in a dictionary with values > 0 with key matching a certain string pattern?
- Query a Julia dictionary for certain values
- How do I combine the keys and values of a Dictionary into one List using LINQ?
- LINQ Source Code Available
- Finding the distinct values of a dictionary
- Select All object values to list from dictionary
- Difference between the returned values of two selector functions in this code
- .NET 4 Code Contracts: "requires unproven: source != null"
- EF Code First comparing null values generates strange query
- Check if values of Dictionary contains an element with certain field value
- Get keys as List<> from Dictionary for certain values
- Linq and Dictionary and converting array values
- Decrement all int values in Dictionary
- How to convert Dictionary to List with mixing of values and keys?
- Compare keys of dictionary with values of another dictionary
- Searching though values of a dictionary
- Grouping 2 Tables, Counting Values And Then Saving The Results In A Dictionary
- Comparing difference between values in dictionary
- Calculating the sum of the values in a dictionary that exist in a generic list
- List of objects to Dictionary with distinct keys and selected values
- Cast troubles while using LINQ to filter a Dictionary by values
More Query from same tag
- getting strongly typed column names from datacontext
- Pass Expression<Func<Type, bool>> as TestCase
- Linq wild card search mvc c#
- How do I add a new record to an IQueryable variable?
- How to use join to make this queries cleaner?
- Dynamic LINQ context
- using linq-like expression in angular ng-show
- ASP.NET MVC Select list value using key
- Help with LINQ query
- Expression Tree for Aggregate Function in LINQ to Entities
- Linq remove where if the value is null
- How to convert and assign result of a linq query to a datatable?
- displaying parent-child elements hierarchically in c#
- How to query a nested list using a lambda expression
- Linq Where local counter closure different results in VS watch
- Sort List except one entry with LINQ
- LINQ - joining two tables and geting the result in a viewmodel that has a different structure
- How can I pass an entity property as a parameter of a LINQ Expression?
- Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.iQueryable<int>'
- Fluent LINQ query with multiple join conditions, one of which being a simple equals and another a less than comparison between ids
- VB.NET :For each loops with Resultset from LINQ 2 SQL
- How to query in .NET EF Core with Linq?
- Linq To Entities - Filter on list
- LINQ to XML query attributes
- Convert string to int in linq query
- OrderBy with a non-transitive IComparer
- How can I add a new text box to my WPF from a DataSet?
- How to write Linq query to check whether List have that id
- calculate distance with linq or subsonic
- Set content of particular Pivot Item