c# - Compare elements of an object resides in two list - one on one using LINQ -


i'm trying find out there better way other enumerable.range in clause compare object's elements 1 on one.

it parallel since we're comparing 1 on 1 here.

for example: house.windows[0].color != house1.windows[0].color , movenext house.windows[1].color != house1.windows[1].color , on...

type same in both list.

    public class house {     string housenmbr;     list<window> windows;  }  public class window {     string wsize;     string wcolor;     bool isenergyeff; }  public static class mymain {     void main()     {         house h1 = new house         {             housenmbr = "1",             windows = new list<window> {                  new window {wsize="1", wcolor = "blue",isenergyeff = true},                 new window {wsize="1", wcolor = "black"},                 new window {wsize="1", wcolor = "red"}             }         };          house h2 = new house         {             housenmbr = "1",             windows = new list<window> {                  new window {wsize="2", wcolor = "blue",isenergyeff = false},                 new window {wsize="2", wcolor = "black"}             }         };          //find diffs...         ienumerable<house> updatesfromhouses = id in h2 //since h2 have updates                                              join pd in h1                                              on id.housenmbr equals pd.housenmbr                                                select new house                                              {                                                  windows = pd.windows.where(                                                     wn => enumerable.range(0, wn.windows.count).all(ctr => wn.isenergyeff != id.windows[ctr].isenergyeff)                                                  ).tolist()                                              };                  } } 

use enumerable.zip if comparing 1 one:

house.windows.zip(house1.windows, (w, w1) => w.color != w1.color); 

this returns collection of boolean values 1 one comparison of colors. can use any(b => !b) check if there non equal color.

keep in mind, both windows lists should have same length (only corresponding elements produce result). so, can start checking length of lists before doing zip. if items count different, lists not same.


in order compare windows should override equals , gethashcode methods:

public class window {     public string size { get; set; }     public string color { get; set; }     public bool isenergysaving { get; set; }      public window() { }      public window(string size, string color, bool isenergysaving)     {         size = size;         color = color;         isenergysaving = isenergysaving;     }      public override bool equals(object obj)     {         window other = obj window;         if (other == null)             return false;          return color == other.color &&                isenergysaving == other.isenergysaving;     }      public override int gethashcode()     {         int hash = 19;                     hash = hash * 23 + color.gethashcode();         hash = hash * 23 + isenergysaving.gethashcode();         return hash;     } } 

here house class improved:

public class house {     public house(string number)     {         number = number;         windows = new list<window>();     }      public string number { get; private set; }     public list<window> windows { get; private set; }      public house withwindow(string size, string color, bool energysaving = false)     {         windows.add(new window(size, color, energysaving));         return this;     } } 

with fluent api can create houses this:

house h1 = new house("1")                 .withwindow("1", "blue", true)                 .withwindow("1", "black")                 .withwindow("1", "red");  house h2 = new house("1")                 .withwindow("2", "blue")                 .withwindow("2", "black"); 

and finding changed , new windows like:

var changedandnewwindows = h2.windows.except(h1.windows); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -