Question

I have this:

var sortName = Request.Params["sortName"];
var query = Request.Params["query"];

Func<UsuarioEndereco, bool> whereClause = (uen => uen.GetPropValue<string>(sortName).Contains(query));

The "uen.GetPropValue<string>(sortName)" will be filled dynamically with the sortName the user typed in the page.

For example, if an user looks for a person named "Joe", the snippet will be:

(uen => uen.namePerson.Contains(Joe))

But, I'm having problems with LINQ Case-sensitive searches. If I type "Joe", I will something. On the other hand, If I type "joe", it bring nothing.

How can I make this "Contains(sortName)" works with Case-Insensitive?? I've tried some things with String.Comparer but it reports errors on build solution.

Thanks!!

Was it helpful?

Solution

I believe the following will generate proper SQL:

 uen=>(uen.GetPropValue<string>(sortName)).ToLower().Contains(query.ToLower()))

OTHER TIPS

If this is really LINQ-to-SQL, try using the SqlMethods.Like method instead of String.Contains.

However, I think the problem is that this is NOT LINQ-to-SQL, because you are using delegates instead of Expression trees. So this is being brought client side, then executed locally ("LINQ to Objects"). Hence, String.Contains is doing what it does locally.

In that way, James's answer is correct, since he's calling ToLower() on both the value and the query. (Although, beware of culture issues -- perhaps specify which culture you want.)

You could also use the String.IndexOf Method (String, Int32, StringComparison) (http://msdn.microsoft.com/en-us/library/ms224424.aspx). This method allows you to specify if the matching should be done case-sensitively or not, and if it should use a Invariant culture or not.

So in your example:

Func<UsuarioEndereco, bool> whereClause = (uen => uen.GetPropValue<string>(sortName).IndexOf(query, 0, StringComparison.OrdinalIgnoreCase));

I'm not commenting on if this is a better solution than the one provided by James Curran. It could or could not be, performance wise.

This is the entire code:

var sortOrder    = Request.Params["sortorder"];    
var sortName     = Request.Params["sortname"];
var query        = Request.Params["query"];

IEnumerable<UsuarioEndereco> pagedEndereco;

Func<UsuarioEndereco, bool> whereClause = (uen => uen.GetPropValue<string>(sortName).Contains(query));
pagedEndereco = sortOrder.Equals("asc", StringComparison.CurrentCultureIgnoreCase) ?
                        _agendaServico.SelecionaUsuarioEnderecos(u.codUsuario).Where(whereClause).OrderByDescending(uen => uen.GetPropValue<IComparable>(sortName)) :
                        _agendaServico.SelecionaUsuarioEnderecos(u.codUsuario).Where(whereClause).OrderBy(uen => uen.GetPropValue<IComparable>(sortName));

The Extension Method GetPropValue is:

public static T GetPropValue<T>(this object component, string propertyName)
{
    return (T)TypeDescriptor.GetProperties(component)[propertyName].GetValue(component);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top