使用LINQ获取流程[]或从Process []获取流程ID[英] Get string[] or process id's from process[] using LINQ

本文是小编为大家收集整理的关于使用LINQ获取流程[]或从Process []获取流程ID的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经打开了五个notepad.exe.现在,我想知道与每个notepad.exe关联的所有过程ID.

我知道我可以通过:

检索记事本流程列表

process [] processes = process.getProcessesbyname(" Notepad");

,但是现在我想要使用LINQ的那些记事本实例的流程ID的字符串[].我怎么做?我知道我可以创建字符串[]并使用foreach循环填充字符串[],但是我想知道使用linq.

推荐答案

您可以使用:

Process[] processes = Process.GetProcessesByName("notepad");
string[] ids = processes.Select(p => p.Id.ToString()).ToArray();

但是,我质疑将其放入string[]类型中的需求.最好只离开 process.id 作为整数的值:

Process[] processes = Process.GetProcessesByName("notepad");
var ids = processes.Select(p => p.Id);
foreach(int processId in ids)
{
   // Do something with each processId
}

其他推荐答案

您可以写

var ids = Array.ConvertAll(Process.GetProcessesByName("notepad"), p => p.Id);

这将比Select和ToArray快(稍微),因为它不需要调整数组大小.

如果您可以使用IEnumerable<int>而不是数组,则Select(无ToArray)会更快.

其他推荐答案

您可以使用数组转换而不是Linq:

进行单线线
int[] processIds = Array.ConvertAll(Process.GetProcessesByName("notepad"), 
                                    p => p.Id);

获取流程ID列表:

Process[] processes = Process.GetProcessesByName("notepad");
var processIdList = processes.Select(p => p.Id).ToList();

获得数组而不是列表,您可以做:

var processIdList = processes.Select(p => p.Id).ToArray();

请注意,该过程ID是类型int.如果您想要一个字符串列表,则可以这样做:

var processIdList = processes.Select(p => p.Id.ToString()).ToArray();

本文地址:https://www.itbaoku.cn/post/1557010.html

问题描述

I have opened five notepad.exe's. Now I want to know all the process id's associated with each notepad.exe.

I know I can retrieve list of notepad processes by:

Process[] processes = Process.GetProcessesByName("notepad");

But now I want string[] of process Id's of those notepad instances using LINQ. How do I do that? I know i can create string[] and using foreach loop populate the string[] but I would like to know using LINQ.

推荐答案

You can use:

Process[] processes = Process.GetProcessesByName("notepad");
string[] ids = processes.Select(p => p.Id.ToString()).ToArray();

However, I question the need to put this into a string[] type. It might be better to just leave the Process.Id values as integers:

Process[] processes = Process.GetProcessesByName("notepad");
var ids = processes.Select(p => p.Id);
foreach(int processId in ids)
{
   // Do something with each processId
}

其他推荐答案

You can write

var ids = Array.ConvertAll(Process.GetProcessesByName("notepad"), p => p.Id);

This will be (slightly) faster than Select and ToArray, since it won't need to resize the array.

If you can use an IEnumerable<int> instead of an array, Select (without ToArray) will be faster.

其他推荐答案

You could do a one-liner using array conversion instead of LINQ:

int[] processIds = Array.ConvertAll(Process.GetProcessesByName("notepad"), 
                                    p => p.Id);

To get a list of process ids:

Process[] processes = Process.GetProcessesByName("notepad");
var processIdList = processes.Select(p => p.Id).ToList();

To get an array instead of a list you can do:

var processIdList = processes.Select(p => p.Id).ToArray();

Note that the process Id is of type int. If you wanted a list of strings instead you could just do this:

var processIdList = processes.Select(p => p.Id.ToString()).ToArray();