请考虑以下代码:
private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine("List of processes @ " + d.ToString()); Process[] localAll = Process.GetProcesses().Where(o => o.ProcessName.ToLower() != "svchost"); if(localAll.Length > 0) { for(int i = 0; i < localAll.Length; i++) { sw.WriteLine(" " + localAll[i].ProcessName); } } }
但我得到一条红色的波浪线说:
无法将类型System.Collections.Generic.IEnumerable'隐式转换为'System.Diagnostics.Process []'.存在显式转换(您是否错过了演员?)
我尝试将数组更改为List但没有工作.
更改
Process[] localAll = Process.GetProcesses().Where(o => o.ProcessName.ToLower() != "svchost");
至
Process[] localAll = Process.GetProcesses().Where(o => o.ProcessName.ToLower() != "svchost").ToArray();
Where
返回一个IEnumerable
你可以调用ToArray
的转换为类型的数组Process
.
另外,您也可以通过枚举IEnumerable
该Where
返回.
var processes = Process.GetProcesses().Where(o => o.ProcessName.ToLower() != "svchost"); foreach (Process process in processes) { sw.WriteLine(" " + process.ProcessName); }