Filtering a list of processes based on a list of string objects
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
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;
精彩评论