c# - Comparing LINQ entities and getting difference -


i'm writing synchronization system database flash drive. so, there xml file on flash content got guid of entity.

i'm using standart xmlserialize , dataloadoptions serialization base , related entities.

so, need implement comparator difference between database data , deserialized data flash drive.

i tried use linq except<> method, says same entities different.

is there anothere way compare entities , object, contains different data?

you can create own method that. let's have :

interface ientity {     int id { get; set; } } 

and concrete class implements it:

class entity : ientity {     public int id { get; set; } } 

you can create method :

    static icollection<t> compare<t>(icollection<t> list1, icollection<t> list2) t : ientity     {         list<t> result = new list<t>();         foreach (t item in list1) if(!list2.any(e => e.id == item.id)) result.add(item);         foreach (t item in list2) if (!list1.any(e => e.id == item.id)) result.add(item);         return result;     } 

usage :

list<ientity> list1 = new list<ientity>() { new entity() { id = 1 }, new entity() { id = 10 } }; list<ientity> list2 = new list<ientity>() { new entity() { id = 1 }, new entity() { id = 5 } };  list<ientity> result = compare(list1, list2).tolist();  foreach (ientity item in result) {     console.writeline(item.id); } 

output : 5, 10

p.s. should ok in terms of performance if still want use linq - try implement iequalitycomparer interface , pass instance of .except() method second overload.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -