c# - Iterating through two collections LINQ -


is possible iterate through 2 collections linq?

i have observablecollection trying fill , @ moment using typical foreach loop.

i use 1 loop loop through a string array, value check if each bool value true , add value collection.

public observablecollection<staffinfo> display = new observablecollection<staffinfo>();      public void masterdatadisplay()     {         string[] data = new[] {"a", "b", "c"};          foreach (var section in data)           {             var filter = db.staffinfos.where(p => p.section == section);             foreach (var item in filter)             {                 if (item.currentstaff == true)                 {                     display.add(item);                 }             }         }     } 

is possible single linq query? or doing best way this?

sure. you're looking items currentstaff property true , section property contained in data collection:

display = new observablecollection<staffinfo>(                  db.staffinfos                    .where(p => p.currentstaff == true                             && data.contains(p.section))               ); 

the nice thing contains creates in clause in sql, can data in 1 query instead of 3.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -