locked
Take Last Records from duplicate data in List<T> RRS feed

  • Question

  • This is my code

    List<Lineitem> _li = new List<Lineitem>
    {
    
    	new Lineitem { LiName = "Net Revenue", TabName = "BCC" },
    	new Lineitem { LiName = "Global Revenue", TabName = "bcf" },
    	new Lineitem { LiName = "Cost of Revenue", TabName = "AXN" },
    	new Lineitem { LiName = "Gross Profit-GAAP", TabName = "POL" },
    	new Lineitem { LiName = "EBITDA", TabName = "BCF" }
    };
    
    var groupedCustomerList1 = _li
    .GroupBy(u => u.TabName.ToUpper())
    .Select(grp => grp.ToList())
    .ToList();
    
    OR
    
    var groupedCustomerList3 = _li
    .GroupBy(u => u.TabName.ToUpper())
    .SelectMany(grp => grp.Skip(1))
    .ToList();

    BCF appear twice in list<T>. so how could i take last records from the duplicate records. above code not return desired output. thanks

    Thursday, November 5, 2020 11:50 AM

Answers

  • This one worked

                var groupedCustomerList1 = _li
                .GroupBy(u => u.TabName.ToUpper())
                .Select(grp => grp.LastOrDefault())
                .ToList();

    but if any other better solution exist then please let me know. thanks

    • Proposed as answer by CoolDadTx Thursday, November 5, 2020 2:25 PM
    • Marked as answer by Sudip_inn Friday, November 6, 2020 10:18 AM
    Thursday, November 5, 2020 12:10 PM
  • Hi Sudip_inn,
    First, I am glad you have got your solution and we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    And then I searched for more methods about this problem, the method that using GroupBy with LastOrDefault is a relatively simple and fast method.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, November 6, 2020 10:18 AM
    Friday, November 6, 2020 7:51 AM

All replies

  • This one worked

                var groupedCustomerList1 = _li
                .GroupBy(u => u.TabName.ToUpper())
                .Select(grp => grp.LastOrDefault())
                .ToList();

    but if any other better solution exist then please let me know. thanks

    • Proposed as answer by CoolDadTx Thursday, November 5, 2020 2:25 PM
    • Marked as answer by Sudip_inn Friday, November 6, 2020 10:18 AM
    Thursday, November 5, 2020 12:10 PM
  • Hi Sudip_inn,
    First, I am glad you have got your solution and we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    And then I searched for more methods about this problem, the method that using GroupBy with LastOrDefault is a relatively simple and fast method.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, November 6, 2020 10:18 AM
    Friday, November 6, 2020 7:51 AM