comparing two generic list
-
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) :)
-
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) :)
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.
-
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.
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
-
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
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|
-
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
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.
-
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.
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