简体   繁体   中英

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?

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;  

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM