Search code examples
linqwhere-clauseoperator-keywordclause

Clause linq with 2 parameter with operator OR don´t retrieve the values correctly


My problem is, I have a linq query with a where clause with 2 parameters, the first parameters comes from the global filter, and the second comes from column filter.

When I enter the value in the global filter, and I don´t enter any value in column filter, (column will be string.empty) , I don´t have any value returned, but I should have.

Seem that every thing is ok, but I don´t see where is the mistake.

string search = Request.Form.GetValues("search[value]")[0];
var column= Request.Form.GetValues("columns[0][search][value]").FirstOrDefault() ?? string.Empty;

            var dataResut = data.Where(
                                p => 
                               p.ProductName.ToLower().Contains(search.ToLower()) || p.ProductName.ToLower().Contains(column.ToLower())
                                ).ToList();

What I am missing here :(

Thanks in advance.

Jolynice


Solution

  • Please try this

    var dataResut = data.Where(
                                p =>
                               (string.IsNullOrEmpty(search) ? false : p.ProductName.ToLower().Contains(search.ToLower())) || (string.IsNullOrEmpty(column) ? false : p.ProductName.ToLower().Contains(column.ToLower()))
                                ).ToList();