简体   繁体   English

EF Core - Select 所有列使用 EF.Functions.Like

[英]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).我正在使用 EF Core 3.1.4(表名混淆)搜索 SQL 服务器数据库的大视图。

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?有没有办法只 select 所有列,而不是在每列的Where条件中添加一个或运算符?

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:一旦你有一个列名列表,你可以编写一个 static 或扩展方法IsLikeAColumn ,像Where(s => IsLikeAColumn(s))一样使用,看起来像:

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.不,这根本不可能,甚至在 SQL 服务器中也是不可能的。 Even in C# If and While also you can not do this.即使在 C# IfWhile你也不能这样做。 You should have multiple conditions with comparison operators比较运算符应该有多个条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core 5 - 如何将 EF.Functions.Like 与映射到 JSON 字符串的自定义属性一起使用? - EF Core 5 - How can I use EF.Functions.Like with a custom property that maps to a JSON string? EF Core - 我们可以使用 EF.Functions.Like 执行动态数量的 OR 运算符吗? - EF Core - Can we do a dynamic number of OR operators with EF.Functions.Like? 实体框架 EF.Functions.Like 与 string.Contains - Entity framework EF.Functions.Like vs string.Contains 如何使 EF.Functions.Like 不区分大小写? - How to make EF.Functions.Like case-insensitive? EF.Functions.Like 在已编译查询中无法转换为 SQL 等效项 - EF.Functions.Like in a compiled query could not be translated to SQL equivalent 升级到 .NET 6 后,EF.Functions.Like 方法不起作用 - EF.Functions.Like method not working after upgrading to .NET 6 Linq WHERE EF.Functions.Like - 为什么直接属性有效而反射无效? - Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not? 有没有办法使用带有ESCAPE sql关键字的EF.Functions.Like来防止通配符 - Is there a way to use EF.Functions.Like with ESCAPE sql keyword to prevent wildcard 选择所有列并返回 EF Core、.NET Core 中的给定类型 - Select All Columns and return a given type in EF Core, .NET Core EF Core 在映射到 Select 中的 object 时查询 SQL 中的所有列 - EF Core queries all columns in SQL when mapping to object in Select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM