I am trying to dynamically create some LINQ query to find data in all columns.

I am currently using the following code to build a dynamic LINQ query. However, it is rather difficult when working with a complex column.
var type = property[col.ToLower()].PropertyType; var isNullableType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>); if (type.IsValueType && !isNullableType) { filter += col + ".ToString().ToLower().Contains(@" + i + ".ToLower())"; } else if (isNullableType) { filter += col + ".HasValue ? " + col + ".Value.ToString().ToLower().Contains(@" + i + ".ToLower())" + " : false"; } else { filter += "(" + col + " != null ? " + col + " : \"\").ToString().ToLower().Contains(@" + i + ".ToLower())"; }
Do you have an idea to simplify my code? I am fine if your suggestion only works for SQL Server 2008 or later.
Note: Column data can be any types, such as integer, string, object, enum, and can be null.
source share