none
Comparing lists of obejcts.

    Question

  • Hi, 

    I have a list of an object and in the setter I want to check if they have nay object that is different. I used I Compare and wrote a compare method and used

    it like this:

     var same = false;
                        var update = true;
                        foreach (var item in List1)
                        {
                            same = false;
                            foreach (var itemNew in value)
                            {
                                if (item.CompareTo(itemNew) == 0)
                                {
                                    same = true;
                                    break;
                                }
                            }
                            if (!same)
                            {
                                update = false;
                            }
                            
                        }
    
    if (!update)
     List = value;

    This somehow looks like non elegant code. Is there a better way?


    Please Mark as Answered If this answers your question Or UnMark as Answered if it did not.
    Happy to Help :)
    My Site

    Monday, April 17, 2017 10:06 AM

All replies

  • Hello,

     In what context is your code applied? Such as, check new object against a List<object>

    I would suggest using Dictionary container, as you can only have one Object Type,

    prevent duplicate entries.

    We can better answer your question if we know more details about the purpose.

     Hope this helps :)


    • Edited by User3DX Monday, April 17, 2017 12:55 PM spelling
    Monday, April 17, 2017 12:53 PM
  • The "simplest" code to do this is to use Any. This is assuming that your type implements IEqualityComparer so you can call CompareTo.

    //Assuming the following is in your class as a field
    private List<SomeType> _items;
    
    //Then the setter becomes pretty straightforward
    {
       //Check for null?
       if (value == null)
           //What should you do here?
    
       if (!_items.Any(x => x.CompareTo(value) == 0))
          _items.Add(value);
    }

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, April 17, 2017 2:02 PM
    Moderator
  • I have a

    List<MyObjectType> list1;

    List<MyObjectType> list2;

    I want to compare them object by object and value by value to make sure that they are identical or not. Order doesn't matter.

    Above code works but looked clumsy to me.


    Please Mark as Answered If this answers your question Or UnMark as Answered if it did not.
    Happy to Help :)
    My Site

    Monday, April 17, 2017 2:28 PM
  • //Find any items in first list that aren't in second
    var missingInSecond = list1.Except(list2);
    var missingInFirst = list2.Except(list1);
    
    if (missingInFirst.Any() || missingInSecond.Any())
       //Some are missing
    This assumes that the type in question implements IEqualityComparer<T>. If it doesn't then the default comparer is used.
    Monday, April 17, 2017 2:50 PM
    Moderator
  • Hello,

    See Enumerable.SequenceEqual


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, April 17, 2017 9:52 PM
    Moderator