lambda expression to sort on min values in a group

Your desired result seems wrong to me…

  1. You only want active records, ok check
  2. The order they were inserted…

3 > 4 > 5 > 6 > 7 not 7 > 5 > 3 > 4 > 6

In the top table Person ID was added again giving it an ExpenseID of 7 which is fine, it should be first (if you are sorting in descending). But then you have PersonID 1 with expense ID 5 when PersonID 5 with expense ID 6 was the previous record added before PersonID 3 with expense id 7

In short, your input data doesn’t match your example desired output, making it rather difficult for anyone to answer you here.

From what I can see it looks like they should be sorted where HistoryCode = 0 Orderd By ExpenseID (bad database design comments aside)…

In such case it would be

var sortedItems = rawItems.Where(w => (w.HistoryCode == 0)).ToList();
sortedItems.Sort((emp1, emp2) => emp2.ExpenseID.CompareTo(emp1.ExpenseID));
//where rawItems is a List<Object> where object has properties for ExpenseID, PersonID... etc    

Here is the entire Console App I made to experiment with this (bored), Add an Xml file called Data with the used Node/Attributes in it and set it to copy to output directory,

    static void Main(string[] args)
    {
        var rawItems = new[] { new { ExpenseID = 0, PersonID = 0, SeqNumb = 0, HistorySeqNumb = 0, HistoryCode = 0 } }.ToList();
        using (FileStream fs = new FileStream(Environment.CurrentDirectory + "\\data.xml", FileMode.Open, FileAccess.Read, FileShare.None))
        {
            XmlReader reader = XmlReader.Create(fs);                
            XDocument doc = XDocument.Load(reader);
            fs.Flush();
            doc.Root.Elements("Item").ToList().ForEach(i =>
            {
                var xExpenseID = Convert.ToInt32(i.Attribute("ExpenseID").Value);
                var xPersonID = Convert.ToInt32(i.Attribute("PersonID").Value);
                var xSeqNumb = Convert.ToInt32(i.Attribute("SeqNumb").Value);
                var xHistorySeqNumb = Convert.ToInt32(i.Attribute("HistorySeqNumb").Value);
                var xHistoryCode = Convert.ToInt32(i.Attribute("HistoryCode").Value);
                rawItems.Add(new { ExpenseID = xExpenseID, PersonID = xPersonID, SeqNumb = xSeqNumb, HistorySeqNumb = xHistorySeqNumb, HistoryCode = xHistoryCode });
            });
        }

        //sort
        var sortedItems = rawItems.Where(w => (w.HistoryCode == 0)).ToList();
        sortedItems.Sort((emp1, emp2) => emp2.ExpenseID.CompareTo(emp1.ExpenseID));

        Console.Write("ExpenseID".PadRight(16, ' '));
        Console.Write("PersonID".PadRight(16, ' '));
        Console.Write("SeqNumb".PadRight(16, ' '));
        Console.Write("HistorySeqNumb".PadRight(16, ' '));
        Console.WriteLine("HistoryCode".PadRight(16, ' '));
        foreach (var item in sortedItems)
        {
            Console.Write(item.ExpenseID.ToString().PadRight(16, ' '));
            Console.Write(item.PersonID.ToString().PadRight(16, ' '));
            Console.Write(item.SeqNumb.ToString().PadRight(16, ' '));
            Console.Write(item.HistorySeqNumb.ToString().PadRight(16, ' '));
            Console.WriteLine(item.HistoryCode.ToString().PadRight(16, ' '));
        }

        Console.ReadKey(true);
    }

Leave a Comment