none
Marge Duplicated Value in List RRS feed

  • Question

  • hi all,

    I have List of object and i want sample way to Marge Duplicated Value in List 

    EX : First List

    ItemID UnitPrice Quantity
    1 10 3
    1 10 4
    2 5 3
    1 10 2
    2 5 1

    Ex : Result

    ItemID UnitPrice Quantity
    1 10 9
    2 5 4




    • Edited by Ahmed_Soft3 Thursday, March 21, 2019 11:41 PM
    Thursday, March 21, 2019 11:36 PM

Answers

  • Hello,

    You need to do a GroupBy as per the following 

    https://stackoverflow.com/questions/16100900/select-multiple-fields-group-by-and-sum

    Go to the post above and examine the answer

    This is from the post and note it's a pattern to learn and adapt too not an exact solution.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ahmed_Soft3 Friday, March 22, 2019 4:42 PM
    Friday, March 22, 2019 12:37 AM
    Moderator

All replies

  • Hello,

    You need to do a GroupBy as per the following 

    https://stackoverflow.com/questions/16100900/select-multiple-fields-group-by-and-sum

    Go to the post above and examine the answer

    This is from the post and note it's a pattern to learn and adapt too not an exact solution.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ahmed_Soft3 Friday, March 22, 2019 4:42 PM
    Friday, March 22, 2019 12:37 AM
    Moderator
  • Hi Ahmed_Soft3,

    Thank you for posting here.

    For your question, you could use Enumerable.Union method. I make a simple example for your reference.

    In my code, when the one of the ItemID, UnitPrice,Quantity is different, the record is different. According to your data, there is no same record, the code would merge all the 7 records of two lists.

    class Program
        {
            public static void Main()
            {
                List<MyList> myLists1 = new List<MyList>();
                myLists1.Add(new MyList { ItemID = 1, UnitPrice = 10, Quantity = 3 });
                myLists1.Add(new MyList { ItemID = 1, UnitPrice = 10, Quantity = 4 });
                myLists1.Add(new MyList { ItemID = 2, UnitPrice = 5, Quantity = 3 });
                myLists1.Add(new MyList { ItemID = 1, UnitPrice = 10, Quantity = 2 });
                myLists1.Add(new MyList { ItemID = 2, UnitPrice = 5, Quantity = 1 });
    
                List<MyList> myLists2 = new List<MyList>();
                myLists2.Add(new MyList { ItemID = 1, UnitPrice = 10, Quantity = 9 });
                myLists2.Add(new MyList { ItemID = 2, UnitPrice = 5, Quantity = 4 });
    
                List<MyList> myLists = myLists1.Union(myLists2, new MyListComparer()).ToList();
            }
    
        }
        public class MyList
        {
            public int ItemID { get; set; }
            public int UnitPrice { get; set; }
            public int Quantity { get; set; }
        }
        class MyListComparer : IEqualityComparer<MyList>
        {
            public bool Equals(MyList L1, MyList L2)
            {
                return L1.ItemID == L2.ItemID && L1.Quantity == L2.Quantity && L1.UnitPrice == L2.UnitPrice;
            }
            public int GetHashCode(MyList list)
            {
                return list.ItemID;
            }
        }

    When I change the last record of myList2 like below, it would return 6 records with out the same one.

      myLists2.Add(new MyList { ItemID = 2, UnitPrice = 5, Quantity = 3 });
     

    If you need change the requirements of the duplicated records, you could change the conditions of MyListComparer class.

    Best Regards,

    Wendy


    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.

    Friday, March 22, 2019 5:26 AM
    Moderator