1

I need to select DeviceTypeID from the below query but I can't because I started my query with .Any() method. Please help me to select DeviceTypeID

var Ids = query.Any(a => a.HospitalDepartments.Any(b => 
                        b.Units.Any(c => c.Devices.Select(f => f.DeviceTypeID)))).ToList();
Gilad Green
  • 35,761
  • 7
  • 54
  • 89
Alex
  • 185
  • 2
  • 3
  • 12

1 Answers1

4

Any checks if there is at least one item in the collection that meets a predicate. From how it looks what you want is a way to collect all the DeviceTypeIDs in all your hierarchy. If so, then you need SelectMany:

var ids = query.SelectMany(a => 
              a.HospitalDepartments.SelectMany(b => 
                  b.Units.SelectMany(c => 
                      c.Devices.Select(f => f.DeviceTypeID)))).ToList();
Graham
  • 7,035
  • 17
  • 57
  • 82
Gilad Green
  • 35,761
  • 7
  • 54
  • 89
  • It's working very well! Thank you so much. Can please kindly please help me to solve this problem .... i am you are smart enough to solve it! Here is my question link: [link](http://stackoverflow.com/questions/43430023/linq-how-to-use-the-exact-array-value-instead-of-contains) – Alex Apr 15 '17 at 18:57