0

End Result

I need to be able to search any database table for all rows that contain the given search string somewhere in any of the columns.

Current Implementation

I'm using anonymous types to handle the case when an anonymous select lambda statement is used. Currently, using reflection, I can loop through the PropertyInfo[] of the given anonymous type, get the values one by one and compare them to my search string.

var type = ob.GetType();
foreach (PropertyInfo prop in type.GetProperties())
{

    var val = prop.GetValue(ob, null);
    if (val.ToString().Contains(search)) return true;
}

The Problem

This implementation works great for anonymous lists. However, when using IQueryables that map to SQL via EntityFramework, it doesn't because lambda expressions cannot build SQL queries that use c# in the query. The only path I've been able to find so far is creating a complex Expression tree to mimic the foreach loop required to loop through and test all of the PropertyInfo items associated with the anonymous object.

Is there a another way to do this or am I going to have to dig through some Expression tree articles?

Tyler Sells
  • 463
  • 4
  • 16
  • 2
    Have you considered using SQL Server's Full Text Search? – mjwills Jan 31 '19 at 20:33
  • 2
    You can create stored procedure that will do the search in everywhere: https://stackoverflow.com/a/436382/1225270 and use that stored procedure to do the search in your code. – arunes Jan 31 '19 at 20:35
  • Thank you both. I will take a look at those. However, I was hoping there was a way to do this code-first using lambdas. I have very little experience running straight SQL scripts from within c# methods – Tyler Sells Jan 31 '19 at 20:42

0 Answers0