tags:

views:

207

answers:

5

how can i make this NOT a loop?

             {
             ManagementObjectSearcher Vquery = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
             ManagementObjectCollection Vcoll = Vquery.Get();
             foreach (ManagementObject mo in Vcoll)
             {
                 System.Diagnostics.Process.Start("http://www.google.com/search?hl=en&q=" + mo["name"].ToString());
             }
A: 

There are a few (fairly pointless) ways involving lists and methods like ForEach - or possibly Select, but you aren't solving a problem here. Just use the loop. It expresses perfectly what you are doing.

One hacky way (I don't recommmend this here):

Vcoll.Cast<ManagementObject>().Select(mo =>
    System.Diagnostics.Process.Start("http://www.google.com/search?hl=en&amp;q="
        + mo["name"].ToString())).ToArray();

not an improvement IMO.

Marc Gravell
+1  A: 

Here you are.

var procs = (from mo in (new ManagementObjectSearcher("SELECT * FROM Win32_VideoController")).Get().OfType<ManagementObject>()
                    select (System.Diagnostics.Process.Start("http://www.google.com/search?hl=en&amp;q=" + mo["name"].ToString()))).ToList();
this. __curious_geek
No; the execution in LINQ is deferred. That doesn't actually *do* anything except set up the chain.
Marc Gravell
Right. changed the code for immediate execution.
this. __curious_geek
its for .net 2.0
NightsEVil
A: 

A common one-liner is:

Vcoll.ForEach( mo -> System.Diagnostics.Process.Start("http://www.google.com/search?hl=en&amp;q=" + mo["name"].ToString()));

Of course ForEach has its own loop inside.

Jonathan Allen
+2  A: 

Here's a very stupid code to avoid foreach:

if( Vcoll.Count > 0)
{
   IEnumerator en = collection.GetEnumerator();
   en.MoveNext();
   System.Diagnostics.Process.Start("http://www.google.com/search?hl=en&amp;q=" + en.Current["name"].ToString());
}

But, if the problem is opening multiple pages, I'd prefer a simple break in the foreach:

foreach (ManagementObject mo in Vcoll)
{
   System.Diagnostics.Process.Start("http://www.google.com/search?hl=en&amp;q=" + mo["name"].ToString());
   break;
}
digEmAll
totally what i needed was a break; thank you so much! i feel pretty retarded... lol thanks any ways
NightsEVil
sometimes, all you need is a break - sounds like ancient wisdom
Simpzon
That is *not* a complete/correct `IEnumerator` usage; you should also check for `IDisposable` and call `Dispose()`. Or better; just use `foreach` with `break`.
Marc Gravell
@Marc: you're right, mine was a sort of provocation not a real solution to adopt (foreach is far clear) ;)
digEmAll
A: 

If all you want to accomplish is not opening pages twice, then use "distinct":

var foundNames = 
  (from ManagementObject mo in new ManagementObjectSearcher("SELECT * FROM Win32_VideoController").Get()
   let name = mo["Name"].ToString()
   where !String.IsNullOrEmpty(name)
   select name).Distinct();

foreach(var name in foundNames)
  Process.Start("http://www.google.com/search?hl=en&amp;q=" + name);
Robert Giesecke