选择与QUOT;定制不同的"使用LINQ列表中的项目 [英] Selecting "custom distinct" items from a List using LINQ

查看:125
本文介绍了选择与QUOT;定制不同的"使用LINQ列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有策略对象的泛型列表。



该列表包含以下数据

  ID policyNumber policySequence otherData 
1 101 1 AAAA
2 101 2 BBBB
3 101 3 CCCC
4 102 1 DDDD
5 103 1 EEEE
6 103 2 FFFF

我要选择包含最高policySequence的一行每个policyNumber,让我结束了以下内容:

  ID policyNumber policySequence创建
3 101 3 CCCC
4 102 1 DDDD
6 103 2 FFFF

我有以下使用的解决方案的foreach,但不知道是否有这样做的LINQ?

 类节目
{一种更简单,更清洁的方式
静态无效的主要(字串[] args)
{
名单,LT;政策与GT; policyList =新的List<政策与GT;
{
新政策{ID = 1,policyNumber = 101,policySequence = 1,otherData =AAAA},
新政策{ID = 2,policyNumber = 101,policySequence = 2, otherData =BBBB},
新政策{ID = 3,policyNumber = 101,policySequence = 3,otherData =CCCC},
新政策{ID = 4,policyNumber = 102,policySequence = 1,otherData =DDDD},
新政策{n = 5,policyNumber = 103,policySequence = 1,otherData =EEEE},
新政策{ID = 6,policyNumber = 103, policySequence = 2,otherData =FFFF}
};

名单,LT;政策与GT; filteredPolicyList =新的List<政策及GT;();

的foreach(在policyList VAR政策)
{
如果(filteredPolicyList.Exists(X =>!x.policyNumber == policy.policyNumber))
{
filteredPolicyList.Add(政策);
}
,否则
{
VAR currentPolicyInFilteredList = filteredPolicyList.Where(X => x.policyNumber == policy.policyNumber)。首先();

如果(policy.policySequence> currentPolicyInFilteredList.policySequence)
{
filteredPolicyList.Remove(currentPolicyInFilteredList);
filteredPolicyList.Add(政策);
}
}
}
}
}

公共类政策
{
公众诠释身份证;
公众诠释policyNumber;
公众诠释policySequence;
公共字符串otherData;
}


解决方案

  VAR maxPolicies = policyList 
.GroupBy(p => p.PolicyNumber)
。选择(GRP => grp.OrderByDescending(p => p.PolicySequence)。首先()) ;


I have a generic List of Policy objects.

The list contains the following data

id  policyNumber   policySequence  otherData
1   101            1               aaaa
2   101            2               bbbb
3   101            3               cccc
4   102            1               dddd
5   103            1               eeee
6   103            2               ffff

I want to select the one row containing the highest policySequence for each policyNumber, so that I end up with the following:

id  policyNumber   policySequence  created
3   101            3               cccc
4   102            1               dddd
6   103            2               ffff

I have a solution below using a foreach, but was wondering if there was an easier, cleaner way to do this in LINQ?

class Program
    {
        static void Main(string[] args)
        {
            List<Policy> policyList = new List<Policy>
                                          {
                                              new Policy {id = 1, policyNumber = 101, policySequence = 1, otherData = "aaaa"},
                                              new Policy {id = 2, policyNumber = 101, policySequence = 2, otherData = "bbbb"},
                                              new Policy {id = 3, policyNumber = 101, policySequence = 3, otherData = "cccc"},
                                              new Policy {id = 4, policyNumber = 102, policySequence = 1, otherData = "dddd"},
                                              new Policy {id = 5, policyNumber = 103, policySequence = 1, otherData = "eeee"},
                                              new Policy {id = 6, policyNumber = 103, policySequence = 2, otherData = "ffff"}
                                          };

            List<Policy> filteredPolicyList = new List<Policy>();

            foreach(var policy in policyList)
            {
                if(!filteredPolicyList.Exists(x => x.policyNumber == policy.policyNumber))
                {
                    filteredPolicyList.Add(policy);
                }
                else
                {
                    var currentPolicyInFilteredList = filteredPolicyList.Where(x => x.policyNumber == policy.policyNumber).First();

                    if (policy.policySequence > currentPolicyInFilteredList.policySequence)
                    {
                        filteredPolicyList.Remove(currentPolicyInFilteredList);
                        filteredPolicyList.Add(policy);
                    }
                }
            }
        }
    }

    public class Policy
    {
        public int id;
        public int policyNumber;
        public int policySequence;
        public string otherData;
    }

解决方案

var maxPolicies = policyList
    .GroupBy(p => p.PolicyNumber)
    .Select(grp => grp.OrderByDescending(p => p.PolicySequence).First());

这篇关于选择与QUOT;定制不同的&QUOT;使用LINQ列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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