c# - Iterate through 2 Lists -


I have a list and the second list of items & lt; T2 & gt; The item's both lists are sorted alphabetically by asset A. I know that the list & lt; T2 & gt; List of items in list & lt; T1 & gt; There is a subset of and any item list & lt; T2 & gt; is present in the list & lt; T1 & gt; Does not exist in .

Let me list & lt; T1 & gt; It needs to be repeated through and whenever it list & lt; T2 & gt; Matching one variable in , every time a change varies. What is the fastest and best way to do this? I am assuming that I need to do it again through both the lists, but I know that forwarding nested will not be right.

For this type of thing, I like to run double for loop. See the examples below.

  var super = new list & lt; Contacts & gt; (); Super. Add (new contact () {name = "John"}); Super. Add (new contact () {name = "Larry"}); Super. Add (new contact () {name = "Smith"}); Super. Add (new contact () {name = "blank"}); Var sub = new list & lt; Contacts & gt; (); Sub Add (new contact () {name = "Larry"}); Sub Add (new contact () {name = "Smith"}); Var subaccount = 0; (Int i = 0; i & lt; super.kount & subcontinent & lt; sub.Count; i ++) {if (super [i] .name == sub [subaccount] .name) {act (Super [i], sub [sub-quote]); SubCount ++; }}  

Where Act (...) does whatever you want to do.

The time the loop increases every super list, but when you find a match, the sub-list only increases.

Note that this only works because of your two beliefs 1) The lists are sorted both and 2) The second list is a subset of the first.


Comments