Beau
Beau

Reputation: 517

EF Core - Select all columns using EF.Functions.Like

I'm searching a large view on a SQL Server database using EF Core 3.1.4 (table names obfuscated).

var query = searchModel.SearchQuery.ToUpper();

list = list.Where(s => EF.Functions.Like(s.name0.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name1.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name2.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name3.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name4.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name5.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name6.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name7.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name8.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name9.ToUpper(), $"%{query}%")
                || EF.Functions.Like(s.name10.ToUpper(), $"%{query}%"));

Is there a way to just select all columns instead of adding an or operator in the Where condition for each column?

Upvotes: 1

Views: 1339

Answers (2)

Wibble
Wibble

Reputation: 796

You may be able to read the schema of the DB through the Entity Framework, using this answer as a starting point. Once you have a list of the column names, you could write a static or extension method IsLikeAColumn, used like Where(s => IsLikeAColumn(s)), looking something like:

bool match = false;
foreach (string columnName in columnNames)
{
    match |= EF.Functions.Like(columnName, $"%{query}%");
}
return match;  

Upvotes: 1

Vivek Nuna
Vivek Nuna

Reputation: 1

No, Its not possible at all and even in SQL server also its not possible. Even in C# If and While also you can not do this. You should have multiple conditions with comparison operators

Upvotes: 0

Related Questions