我怎样才能使它不是一个循环?

发布于 2024-09-04 04:23:43 字数 442 浏览 2 评论 0原文

我怎样才能使它不是一个循环?

             {
             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());
             }

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());
             }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吖咩 2024-09-11 04:23:43

这是一个避免使用 foreach 的非常愚蠢的代码:

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

但是,如果问题是打开多个页面,我更喜欢在 foreach 中进行简单的中断:

foreach (ManagementObject mo in Vcoll)
{
   System.Diagnostics.Process.Start("http://www.google.com/search?hl=en&q=" + mo["name"].ToString());
   break;
}

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&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&q=" + mo["name"].ToString());
   break;
}
紅太極 2024-09-11 04:23:43

给你。

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&q=" + mo["name"].ToString()))).ToList();

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&q=" + mo["name"].ToString()))).ToList();
数理化全能战士 2024-09-11 04:23:43

有几种(相当无意义的)方法涉及列表和方法,例如 ForEach - 或者可能是 Select,但您在这里并没有解决问题。只需使用循环即可。它完美地表达了您正在做的事情。

一种古怪的方法(我在这里推荐这种方法):

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

在我看来,不是改进。

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&q="
        + mo["name"].ToString())).ToArray();

not an improvement IMO.

顾忌 2024-09-11 04:23:43

常见的一句台词是:

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

当然 ForEach 内部有自己的循环。

A common one-liner is:

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

Of course ForEach has its own loop inside.

2024-09-11 04:23:43

如果您想要完成的只是不打开页面两次,那么使用“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&q=" + name);

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&q=" + name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文