C# comparing 2 lists with string array property -


i have class 1 property of list<string> hold dynamic list of 1 or more string ids.

public class fieldcompareitem {     public list<string> fields = new list<string>();      public fieldcompareitem(string[] fields)     {         (int = 0; < fields.count(); i++)              fields.add(fields[i]);         }     } } 

i'm trying compare 2 lists see if string arrays match doesn't work. basically, want a/b compare items exist in a, in b, , in both, this:

var lista = new list<fieldcompareitem>         {             new fieldcompareitem(new[] {"a1"}),             new fieldcompareitem(new[] {"a2"}),             new fieldcompareitem(new[] {"a3","001"})         };  var listb = new list<fieldcompareitem>         {             new fieldcompareitem(new[] {"a2"}),             new fieldcompareitem(new[] {"a3"}),             new fieldcompareitem(new[] {"a3","001"}),             new fieldcompareitem(new[] {"a4"}),             new fieldcompareitem(new[] {"a5"})         };  //exists in var aonly = lista.except(listb).tolist(); //expect a1,a3  //exists in b var bonly = listb.except(lista).tolist();  //expect a4,a5  //exists in both - may used update a>b or b>a var inboth = ????? //expect a2 

because values within array property doesnt seem find criteria. appreciated

create comparer first:

public class fieldcompareitemcomparer: iequalitycomparer<fieldcompareitem> {   public bool equals(fieldcompareitem x, fieldcompareitem y)   {     var result = x.fields.sequenceequal(y.fields);     return result;   }    public int gethashcode(fieldcompareitem obj)   {     return string.concat(obj.fields).gethashcode();   } } 

then use following:

var comparer = new fieldcompareitemcomparer();  // exists in var aonly = lista.except(listb, comparer).tolist();  // exists in b var bonly = listb.except(lista, comparer).tolist();  // exists in both var inboth = lista.intersect(listb, comparer).tolist(); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -