tags:

views:

53

answers:

3

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;
    }
+3  A: 
var maxPolicies = policyList
    .GroupBy(p => p.PolicyNumber)
    .Select(grp => grp.OrderByDescending(p => p.PolicySequence).First());
Lee
+2  A: 

If you're using LINQ to Objects, you could use the MoreLINQ project's DistinctBy method:

var maxPolicies = policyList.OrderByDescending(x => x.PolicySequence)
                            .DistinctBy(x => x.PolicyNumber);
Jon Skeet
A: 

You could group and aggregate:

var result = from p in policyList
             group p by p.policyNumber into g
             select new { Policy = g.Key, Max = g.Max() };
Darin Dimitrov