Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. comparing two generic list

comparing two generic list

Scheduled Pinned Locked Moved C#
linq
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    enginco
    wrote on last edited by
    #1

    public class g1
    {
    public int OID {get;set;}
    public string Name{get;set;}
    public bool IsActive {get;set;}
    }

    IQueryable<g1> _all;
    _all = GetAll_g1().AsQueryable();

    List<g1> partOfAll = _all.where(p => p.IsActive == true).ToList();

    I have a main list and a part of it's. I changed a g1 from partOfAll // old: Name = "x", IsActive = true new: Name = "x", IsActive = false I want to compare these two list and set to main list which one is changed in partOfAll thanx (sorry for my english) :)

    G 1 Reply Last reply
    0
    • E enginco

      public class g1
      {
      public int OID {get;set;}
      public string Name{get;set;}
      public bool IsActive {get;set;}
      }

      IQueryable<g1> _all;
      _all = GetAll_g1().AsQueryable();

      List<g1> partOfAll = _all.where(p => p.IsActive == true).ToList();

      I have a main list and a part of it's. I changed a g1 from partOfAll // old: Name = "x", IsActive = true new: Name = "x", IsActive = false I want to compare these two list and set to main list which one is changed in partOfAll thanx (sorry for my english) :)

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can't find out which one has changed by comparing the lists, as the lists only contains references to the objects. There is only one copy of each object, not one copy for each list. When you change the object, the change is visible from both lists, as they both reference the same object.

      Despite everything, the person most likely to be fooling you next is yourself.

      E 1 Reply Last reply
      0
      • G Guffa

        You can't find out which one has changed by comparing the lists, as the lists only contains references to the objects. There is only one copy of each object, not one copy for each list. When you change the object, the change is visible from both lists, as they both reference the same object.

        Despite everything, the person most likely to be fooling you next is yourself.

        E Offline
        E Offline
        enginco
        wrote on last edited by
        #3

        Guffa wrote:

        When you change the object, the change is visible from both lists, as they both reference the same object.

        are you sure about this. Im sure that they are not both reference the same object. in this case mainlist is unaffected from any changes, because ".ToList()" returns new copies.

        modified on Monday, July 21, 2008 12:13 PM

        L G 2 Replies Last reply
        0
        • E enginco

          Guffa wrote:

          When you change the object, the change is visible from both lists, as they both reference the same object.

          are you sure about this. Im sure that they are not both reference the same object. in this case mainlist is unaffected from any changes, because ".ToList()" returns new copies.

          modified on Monday, July 21, 2008 12:13 PM

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          enginço wrote:

          Im sure that they are not both reference the same object.

          Good for you. Try it and be absolutely sure, it only takes some 10 lines of code to prove you right or wrong. :wtf:

          Luc Pattyn [Forum Guidelines] [My Articles]


          Voting for dummies? No thanks. X|


          1 Reply Last reply
          0
          • E enginco

            Guffa wrote:

            When you change the object, the change is visible from both lists, as they both reference the same object.

            are you sure about this. Im sure that they are not both reference the same object. in this case mainlist is unaffected from any changes, because ".ToList()" returns new copies.

            modified on Monday, July 21, 2008 12:13 PM

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            enginço wrote:

            are you sure about this.

            Yes, I am absolutely sure about this. To be really really absolutely 100% sure, I verified that the lists contains the same objects, and that changes to the object in one list is visible through the other list.

            enginço wrote:

            in this case mainlist is unaffected from any changes, because ".ToList()" returns new copies.

            No, it doesn't. There is no constraint that the class in the generic enumeration has to have a public parameterless constructor or implement the IClonable interface, so the method can't create copies.

            Despite everything, the person most likely to be fooling you next is yourself.

            E 1 Reply Last reply
            0
            • G Guffa

              enginço wrote:

              are you sure about this.

              Yes, I am absolutely sure about this. To be really really absolutely 100% sure, I verified that the lists contains the same objects, and that changes to the object in one list is visible through the other list.

              enginço wrote:

              in this case mainlist is unaffected from any changes, because ".ToList()" returns new copies.

              No, it doesn't. There is no constraint that the class in the generic enumeration has to have a public parameterless constructor or implement the IClonable interface, so the method can't create copies.

              Despite everything, the person most likely to be fooling you next is yourself.

              E Offline
              E Offline
              enginco
              wrote on last edited by
              #6

              yes you are right, but I was absolutely sure before write. I tried two times to be sure. methinks, I lost in my codes. IT'S MY FAULT. Hmmm, my eyes may be broken but where is my rationality? may be I should less work. I tried if you know or don't :P over and over again I'm sorry

              modified on Tuesday, July 22, 2008 3:25 AM

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups