Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中执行json查询?_C#_Json_Sql Server_Asp.net Core - Fatal编程技术网

如何在C#中执行json查询?

如何在C#中执行json查询?,c#,json,sql-server,asp.net-core,C#,Json,Sql Server,Asp.net Core,我正在使用SQL Server 2017,它支持Json。但是我使用的是EF core,它不能查询json 那我该怎么做呢 范例 SELECT *, JSON_VALUE (Attributes, '$.year') AS Year FROM items WHERE JSON_VALUE(Attributes, '$.year') = '2017' 我如何在C#/ASP.NET Core中查询这个问题?ADO.NET?我假定您的项是如下所示的

我正在使用SQL Server 2017,它支持Json。但是我使用的是EF core,它不能查询json

那我该怎么做呢

范例

  SELECT
      *, JSON_VALUE (Attributes, '$.year') AS Year
  FROM 
      items
  WHERE 
      JSON_VALUE(Attributes, '$.year') = '2017'

我如何在C#/ASP.NET Core中查询这个问题?ADO.NET?

我假定您的
项是如下所示的模型:

    public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Attributes { get; set; }
}
如果是这样,并且您不需要
Year
,您可以从SQL
中尝试
,如下所示:

string sQuery = "SELECT *, JSON_VALUE (Attributes, '$.year') AS Year FROM  Customer WHERE JSON_VALUE(Attributes, '$.year') = '2018'";               

var customerEF = await _context.Customer.FromSql(sQuery).ToListAsync();
这样,您只能返回在
Customer
模型中定义的列

如果您想返回
Customer
Year
列中的列,我建议您尝试以下操作:

using (var connection = new SqlConnection(_configuration.GetConnectionString("DefaultConnection")))
        {                
            string sQuery = "SELECT *, JSON_VALUE (Attributes, '$.year') AS Year FROM  Customer WHERE JSON_VALUE(Attributes, '$.year') = '2018'";
            var customerDapper = connection.QueryFirstOrDefault<CustomerVM>(sQuery);                           
        }
更新

避免SQL操作

尝试传递SQL参数

string sQuery = "SELECT *, JSON_VALUE (Attributes, '$.year') AS Year FROM  Customer WHERE JSON_VALUE(Attributes, '$.year') = @Year";
var year = new SqlParameter("Year", "2018");
var customerEF = await _context.Customer.FromSql(sQuery, year).ToListAsync();

请参考此链接。您不能在sqlserver端编写storedproc并从EF调用它吗?您必须编写原始查询,请参见此处:@DavidG-我看到了,但让我困惑的是,因为它是json,所以我的模型应该包含每个json属性吗?它们是如何映射在一起的?您只能对在您的上下文中已经是
DbSet
的类型进行映射。如果你想运行其他特别的查询,你需要EF核心之外的另一种方法。是的,这就是我最终使用EF的方法,然后一旦我得到了属性,我就需要json.net解析它。现在我在where caluse中硬编码了2018。我如何才能在其中放入一个不会打开sql注入攻击的参数?@chobo2您可以尝试传递参数以避免sql注入攻击。检查我的更新答案。
string sQuery = "SELECT *, JSON_VALUE (Attributes, '$.year') AS Year FROM  Customer WHERE JSON_VALUE(Attributes, '$.year') = @Year";
var year = new SqlParameter("Year", "2018");
var customerEF = await _context.Customer.FromSql(sQuery, year).ToListAsync();