查找字典的不同值 [英] Finding the distinct values of a dictionary

查看:70
本文介绍了查找字典的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GroupSummary class,它具有一些类似的属性:

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; }
}

然后我有一个Dictionary<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

据我所知,我可以说mydictionartItem.select(t => t).Distinct().ToList()之类的东西,但是我认为这将比较该类的所有属性,这是错误的.那我该怎么解决呢?

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?

推荐答案

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());

这篇关于查找字典的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆