sql where-in不适用于list< string>

wn9m85ua  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(318)

当我直接运行sql查询时,我必须解决以下问题:

SELECT [hostname]
  FROM [ComputerImport]
  where LOWER(SUBSTRING (computerimport.hostname, 1, 2)) in ('DE', 'FR') AND computerimport.hostname LIKE '%'

但当我试着离开c#我没有得到任何结果

string searchString = ""
List<string> allowedStatus = new List<string>();
allowedStatus.Add("de");
            string sqlquery = "SELECT computerimport.hostname FROM ComputerImport WHERE LOWER(SUBSTRING (computerimport.hostname, 1, 2)) in ({1}) AND (computerimport.hostname LIKE '%' + {0} + '%') ";                          

_context.OverviewQueries.FromSqlRaw(sqlquery, searchString, string.Join(",", allowedStatus.Select(s => "'" + s + "'").ToArray()));

我也试过:

string.Join(",", allowedStatus.Select(s => "'" + s + "'")
string.Join(",", allowedStatus)
string.Join(",", allowedStatus)ToArray()

有效的方法是:

_context.OverviewQueries.FromSqlRaw(sqlquery, searchString, "DE");

你知道我错过了什么吗?

e5njpo68

e5njpo681#

您可以使用基本的linq,fromsqlraw更适合于使用表值函数等。

string searchString = ""
List<string> allowedStatus = new List<string>();
allowedStatus.Add("de");

return _context.ComputerImport
  .Where(o => o.hostname != null && allowedStatus.Contains(o.hostname.Substring(0,2).ToLower()))
  .Where(o => o.hostname.Contains(searchString));

相关问题