I'm doing this and was just wondering as I'm new to all of this if Linq could be used to rewrite this?
private void checkacrobat()
{
Process[] prs = Process.GetProcesses();
foreach(Process pr in prs)
{
if(pr.ProcessName == "****ProcessName")
pr.Kill();
}
}
foreach(var process in Process.GetProcesses().Where(p=>p.ProcessName==whatever))
process.Kill();
Remember, use statements for actions like killing a process. Use LINQ for queries, that read values without changing them.
But your code is perfectly readable as it is. I wouldn't change it without a good reason.
In method syntax:
var processesToBeKilled = Process.GetProcesses()
.Where(pr => pr.ProcessName == "****ProcessName");
foreach(var process in processesToBeKilled)
process.Kill();
In query syntax:
var processesToBeKilled = from pr in Process.GetProcesses()
where pr.ProcessName == "****ProcessName"
select pr;
foreach(var process in processesToBeKilled)
process.Kill();
Purpose-built method:
There's really no need for LINQ here; there's already the handy Process.GetProcessesByName
method:
var processesToBeKilled = Process.GetProcessesByName("****ProcessName");
foreach(var process in processesToBeKilled)
process.Kill();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With