How to Write In Clause with EF FromSql?

前端 未结 1 1069
余生分开走
余生分开走 2021-01-25 13:33

I have an unknown amount of parameters for my inclause. How can I write it so it works with EF Core?

  var formattedValues = String.Join(\",\", values.Select(s =         


        
相关标签:
1条回答
  • 2021-01-25 14:05

    Since you're using SQL Server, you can pass the list of values as JSON. EG

      var jsonValues = JsonConvert.SerializeObject(values.ToList());
      var filterValueParam = new SqlParameter("@values", jsonValues );
      var identifierParam = new SqlParameter("@jsonColumn", $"$.{identifierKey}");
    
      var sql = "select * FROM Items WHERE  AND JSON_VALUE(Attributes, @jsonColumn) in (select value from openjson(@values))";
    
      var items = dbContext.Items.FromSql(sql, filterValueParam, identifierParam).ToList();
    
    0 讨论(0)
提交回复
热议问题