C# LINQ query problem (case insensitive)

R

radioworld

Hi All,

I just started playing with C# .NET, experimenting with ASP.NET..

What I want to do is to check if there are certain processes running.

The statement below returns all the processes:
var query = from p in System.Diagnostics.Process.GetProcesses()
select p;


What I am trying to do is to filter only those processnames that have
"firewall".

I noticed that you cannot use something such as LIKE "%firewall%".
There is a Contains() function but that one is case-sensitive..

Although there is a process named "Windows7FirewallService", the query
below doesn't contain nothing :-/

var query = from p in System.Diagnostics.Process.GetProcesses()
where p.ProcessName.Contains("firewall")
select p;


Does anyone have a suggestion on how to alter this code?

Thanks




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var query = from p in
System.Diagnostics.Process.GetProcesses()
where p.ProcessName.Contains("firewall")
select p;

// 'Windows7FirewallService' is running, but not showing
up here
foreach (System.Diagnostics.Process p in query)
{
Response.Write(p.ProcessName + "<br/>");
}
}
}
}
 
F

Felix Palmen

* radioworld said:
What I am trying to do is to filter only those processnames that have
"firewall".

I noticed that you cannot use something such as LIKE "%firewall%".
There is a Contains() function but that one is case-sensitive..

Make sure you understand the concept of linq. It basically uses
predicates and selectors (little anonymous callback functions that
return a boolean or an element) as parameters to its functions. (*)

Where is such a linq function and will accept any predicate returning a
boolean. So, of course you can use Contains() for that (but that's still
just a regular System.String method). If you have different needs, maybe
use a Regex.Match() instead like ...

Regex expression = new Regex("firewall", RegexOptions.IgnoreCase);
[...]

.... where expression.IsMatch(p.ProcessName) ...

Regards,
Felix

(*) Maybe this is easier to understand when you also try the alternative
syntax of directly invoking linq's function instead of using the
"query-like" syntax:

[...].GetProcesses().Where(p => expression.IsMatch(p.ProcessName))
 
K

kndg

Hi All,

I just started playing with C# .NET, experimenting with ASP.NET..

What I want to do is to check if there are certain processes running.

The statement below returns all the processes:
var query = from p in System.Diagnostics.Process.GetProcesses()
select p;


What I am trying to do is to filter only those processnames that have
"firewall".

I noticed that you cannot use something such as LIKE "%firewall%".
There is a Contains() function but that one is case-sensitive..

Although there is a process named "Windows7FirewallService", the query
below doesn't contain nothing :-/

var query = from p in System.Diagnostics.Process.GetProcesses()
where p.ProcessName.Contains("firewall")
select p;


Does anyone have a suggestion on how to alter this code?

Thanks

where p.ProcessName.IndexOf("firewall", 0,
StringComparison.InvariantCultureIgnoreCase) >= 0

or

where p.ProcessName.ToLowerInvariant().Contains("firewall")

would do the trick.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top