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
  • 36,708
  • 7
  • 61
  • 95
Alex
  • 195
  • 3
  • 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,431
  • 18
  • 59
  • 84
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • 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