c# - Use linq to filter by property of subcollection -


class store {     public int id { get; set; }     public list<annualdata> annualdata { get; set; } }  class annualdata {     public int year { get; set; }     public dictionary<string, double> data { get; set; } } 

i need requested stores , requested years' data. can filter stores easily:

list<int> storeids = new list<int> {12, 13, 14}; list<store> stores = alldata.stores.where(s => storeids.contains(s.id)).tolist(); 

but can't figure out how further filter list of years.

list<int> years = new list<int> {2012, 2013, 2014}; stores = alldata.stores.where(s => storeids.contains(s.id)).????? 

tried any/all/contains i'm not understanding linq enough. in advance.

update after proposed solutions selmann & timothy using examples still see 3 years store 1 in output - want see 2007 in t case:

   static void main(string[] args)     {         list<store> alldata = new list<store> {                              new store { id = 1,                         annualdata = new list<annualdata> {                             new annualdata {                                 year = 2006,                                 data = new dictionary<string, double>{{"value2006", 1.0}}                             },                             new annualdata {                                 year = 2007,                                 data = new dictionary<string, double>{{"value2007", 1.1}}                             },                             new annualdata {                                 year = 2008,                                 data = new dictionary<string, double>{{"value2008", 1.2}}                             }                         }             }         };          list<int> storeids = new list<int> { 1 };         list<int> years = new list<int> { 2007 };          var stores = alldata.where(s => storeids.contains(s.id) &&                                      s.annualdata.any(x => years.contains(x.year)));          foreach (store s in stores)             foreach (annualdata in s.annualdata)                 console.writeline(a.year);      } 

stores = alldata.stores     .where(s => storeids.contains(s.id))     .select(s => new store     {         id = s.id,         annualdata = s.annualdata             .where(x => years.contains(x.year))             .tolist()     }); 

Comments

Popular posts from this blog

php - Magento - Deleted Base url key -

javascript - Tooltipster plugin not firing jquery function when button or any click even occur -

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -