Filtering a list of processes based on a list of string objects

余生颓废 提交于 2021-02-05 10:48:43

问题


I have a List of string objects which represent process names. I want to filter a collection of running Processes (retrieved by using GetProcesses()), by using the list of string objects I have. So if I want to look for the Sql Server process running in the processes collection, I will look for the string name as stored in the string list.

How can I filter a list of processes to get only the processes which have the same process names as a list of strings (the different generic types makes it hard - for me, anyway)?

I am using .NET 4.0 and LINQ.

Thanks


回答1:


This should do it..

var targetNames = new [] { "processone", "Processtwo" };

var processes = from p in Process.GetProcesses()
                where targetNames.Any(n => n == p.ProcessName)
                select p;


来源:https://stackoverflow.com/questions/2304326/filtering-a-list-of-processes-based-on-a-list-of-string-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!