What is the most efficient way to search for text of any type of data in EF6?

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

enter image description here

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.

+5
source share
1 answer

If you want to write an Entity Framework query that implements a keyword search that returns strings whose description contains some or all of a given set of keywords, you can use PredicateBuilder .

To create a dynamic predicate from the read line:

Using the where where clause with PredicateBuilder in LinqPad to create a dynamic where clause

LINQ and dynamic predicate construction at runtime

-1
source

Source: https://habr.com/ru/post/1209732/


All Articles