根据字符串对象列表过滤进程列表 [英] Filtering a list of processes based on a list of string objects

查看:43
本文介绍了根据字符串对象列表过滤进程列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表进程名称的字符串对象列表.我想通过使用我拥有的字符串对象列表来过滤正在运行的进程的集合(通过使用GetProcesses()进行检索).因此,如果我要查找在processs集合中运行的Sql Server进程,则将查找存储在字符串列表中的字符串名称.

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)?

我正在使用.NET 4.0和LINQ.

I am using .NET 4.0 and LINQ.

谢谢

推荐答案

这应该做到.

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

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

这篇关于根据字符串对象列表过滤进程列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆