Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the distinct values of a dictionary

Tags:

c#

linq

I have a GroupSummary class that has some properties like this in it:

public class GroupsSummary
{
    public bool FooMethod()
    {
      ////
    }
    public bool UsedRow { get; set; }

    public string GroupTin   { get; set; }
    public string PayToZip_4 { get; set; }
    public string PayToName  { get; set; }

    public string PayToStr1     { get; set; }
    public string PayToStr2     { get; set; }
    public string PayToCity     { get; set; }
    public string PayToState    { get; set; }
    public bool   UrgentCare_YN { get; set; }
}

Then I have a Dictionary like <string, List<GroupsSummary>

For each of these dictionary items I want to find all the distinct addresses but the properties of this class that define a distinct address for me are

PayToStr1,PayToStr2,PayToCity,PayToState

I know as far as I can say something like mydictionartItem.select(t => t).Distinct().ToList() but I think that will compare all the properties of this class which is wrong. So how should I solve this?

like image 487
ConfusedSleepyDeveloper Avatar asked Aug 01 '14 19:08

ConfusedSleepyDeveloper


People also ask

How do you find unique values in a dictionary?

Method #1 : Using set() + values() + dictionary comprehension The combination of these methods can together help us achieve the task of getting the unique values. The values function helps us get the values of dictionary, set helps us to get the unique of them, and dictionary comprehension to iterate through the list.

What are the distinct properties of a dictionary?

Properties of Dictionary KeysThe values in the dictionary can be of any type, while the keys must be immutable like numbers, tuples, or strings. Dictionary keys are case sensitive- Same key name but with the different cases are treated as different keys in Python dictionaries.

How do you find the value in a dictionary?

The well-known, or I should say the traditional way to access a value in a dictionary is by referring to its key name, inside a square bracket. Notice that when you want to access the value of the key that doesn't exist in the dictionary will result in a KeyError.


1 Answers

var newDict = dict.ToDictionary(
                    x=>x.Key, 
                    v=>v.Value.GroupBy(x=>new{x.PayToStr1, x.PayToStr2, x.PayToCity, x.PayToState})
                              .Select(x=>x.First())
                              .ToList());
like image 141
L.B Avatar answered Sep 20 '22 05:09

L.B