ManagementObjectCollection Indexers लागू करता है नहीं है, लेकिन हाँ आप कर सकते हैं आप FirstOrDefault विस्तार समारोह यदि आप LINQ लेकिन गीक्स जो .net 3 या उससे पहले (प्रयोग कर रहे हैं मेरे जैसे अभी भी प्रयोग कर रहे हैं 1.1 पर काम कर रहा है) निम्नलिखित कोड का उपयोग कर सकते हैं, यह किसी भी संग्रह से लागू प्रथम आइटम प्राप्त करने का मानक तरीका है INumerable इंटरफेस।
//TODO: Do the Null and Count Check before following lines
IEnumerator enumerator = collection.GetEnumerator();
enumerator.MoveNext();
ManagementObject mo = (ManagementObject)enumerator.Current;
के बाद किसी भी सूचकांक से ManagementObject पुनः प्राप्त करने के दो अलग अलग तरीकों रहे
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");
}
या
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");
}
स्रोत
2013-11-27 00:47:29
मैं 'जोड़ने के लिए OfType' ...' ManagementObject मो = queryCollection.OfType <जरूरत । ManagementObject>() पहले();। ' –
मैं' नहीं दिख रहा है FirstOrDefault() '' ManagementObject' में या तो, केवल 'OfType() FirstOrDefault()' मुझे –
Jack
के लिए काम किया किसी के लिए जो जैसा कि मैं था, उलझन में है, इस जवाब में एक टाइपो है। यह पढ़ना चाहिए:। 'ManagementObject मो = queryCollection.OfType() FirstOrDefault()' –
SGS