10

I'm trying to access ManagementObjects in ManagementObjectCollection without using a foreach statement, maybe I'm missing something but I can't figure out how to do it, I need to do something like the following:

ManagementObjectSearcher query = new ManagementObjectSearcher(
     "select Name, CurrentClockSpeed from Win32_Processor");

ManagementObjectCollection queryCollection = query.Get();

ManagementObject mo = queryCollection[0];
x0n
  • 51,312
  • 7
  • 89
  • 111
Jamesla
  • 1,378
  • 7
  • 31
  • 62

3 Answers3

19

ManagementObjectCollection implements IEnumerable or ICollection, so either you must iterate it via IEnumerable (ie foreach) or CopyTo an array via ICollection.

However since it supports IEnumerable you can use Linq :

ManagementObject mo = queryCollection.OfType<ManagementObject>().FirstOrDefault()

OfType<ManagementObject> is required because ManagementObjectCollection supports IEnumerable but not IEnumerable of T.

C. Ross
  • 31,137
  • 42
  • 147
  • 238
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 2
    I needed to add `OfType`... `ManagementObject mo = queryCollection.OfType().First();` – Ryan_S Nov 14 '14 at 03:21
  • 1
    I don't see `FirstOrDefault()` in `ManagementObject` either, only `OfType().FirstOrDefault()` worked for me – Jack Apr 25 '16 at 19:30
  • 1
    For anyone who is as confused as I was, there's a typo in this answer. It should read: `ManagementObject mo = queryCollection.OfType().FirstOrDefault()` – SGS Jun 01 '17 at 14:58
4

You can not directly call linq from ManagementObjectCollection (nor an integer indexer). You have to cast it to IEnumerable first:

var queryCollection = from ManagementObject x in query.Get()
                      select x;

var manObj = queryCollection.FirstOrDefault();
TomB
  • 641
  • 5
  • 17
1

ManagementObjectCollection does not implements Indexers, but yes you can you FirstOrDefault extension function if you are using linq but geeks who are using .net 3 or earlier (like me still working on 1.1) can use following code, it is standard way of getting first item from any collection implemented IEnumerable interface.

//TODO: Do the Null and Count Check before following lines
IEnumerator enumerator = collection.GetEnumerator();
enumerator.MoveNext();
ManagementObject mo = (ManagementObject)enumerator.Current;

following are two different ways to retrieve ManagementObject from any index

private ManagementObject GetItem(ManagementObjectCollection collection, int index)
{
            //TODO: do null handling 

            IEnumerator enumerator = collection.GetEnumerator();

            int currentIndex = 0;
            while (enumerator.MoveNext())
            {
                if (currentIndex == index)
                {
                    return enumerator.Current as ManagementObject;
                }

                currentIndex++;
            }

            throw new ArgumentOutOfRangeException("Index out of range");
 }

OR

private ManagementObject GetItem(ManagementObjectCollection collection, int index)
{
            //TODO: do null handling 

            int currentIndex = 0;
            foreach (ManagementObject mo in collection)
            {
                if (currentIndex == index)
                {
                    return mo;
                }

                currentIndex++;
            }

            throw new ArgumentOutOfRangeException("Index out of range");
 }
Mubashar
  • 12,300
  • 11
  • 66
  • 95