Search single value in multiple columns with Linq.Dynamic.Core

.net-core c# dynamic-linq linq

Question

My test case scenario is about a search textbox and a grid with the search results.

By using the following currently, i search only on the first column.

foreach (GridColumn s in columns)
{
   data = data.Where(string.Format("{0}.Contains(@0)", s.PropertyName), searchValue);
   break; 
}

As you already guessed, if i remove the break statement, no results are returned due to the lack of OR statements between the foreach loop.

How can i search for a single value in multiple columns with Linq.Dynamic.Core?

1
0
3/18/2020 2:53:07 PM

Accepted Answer

With System.Linq.Dynamic.Core you could use code like this:

var xx = new [] { "ax", "bx", "cy", "dz" };

var columns = new[] { "x", "y" };

string query = string.Join(" or ", columns.Select(c => $"it.Contains(\"{c}\")"));

var result = xx.AsQueryable().Where(query);

Example in LinqPad: enter image description here

1
3/19/2020 11:03:06 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow