Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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# 当数组参数的值为null时,无法确定其类型_C#_Postgresql_Dapper_Npgsql - Fatal编程技术网

C# 当数组参数的值为null时,无法确定其类型

C# 当数组参数的值为null时,无法确定其类型,c#,postgresql,dapper,npgsql,C#,Postgresql,Dapper,Npgsql,使用PostgreSQL数据库和Dapper 下面是失败代码的一个最小示例: await using var connection = new NpgsqlConnection(_configuration.ConnectionString); return await connection.QueryAsync<Diamond>( @"SELECT id, name FROM product WHERE @Nam

使用PostgreSQL数据库和Dapper

下面是失败代码的一个最小示例:

await using var connection = new NpgsqlConnection(_configuration.ConnectionString);

return await connection.QueryAsync<Diamond>(
  @"SELECT
      id,
      name
    FROM
      product
    WHERE
      @Names IS NULL OR name = ANY(@Names);",
  new
  {
      Names = Names,
  });

是否可以将Dapper中的可空数组参数发送到PostgreSQL,或者我是否需要找到解决方法?

@PeterCsala是正确的。问题是PostgreSQL准备了查询服务器端,因此在C world中强制转换参数没有帮助。强制转换必须在SQL world中完成

工作代码变为:

await using var connection = new NpgsqlConnection(_configuration.ConnectionString);

return await connection.QueryAsync<Diamond>(
  @"SELECT
      id,
      name
    FROM
      product
    WHERE
      @Names::text[] IS NULL OR name = ANY(@Names);",
  new
  {
      Names = Names,
  });

这里讨论了非常类似的问题,但postgre驱动程序是另一个。谢谢-链接帮助解决了这个问题感谢与社区共享解决方案:
await using var connection = new NpgsqlConnection(_configuration.ConnectionString);

return await connection.QueryAsync<Diamond>(
  @"SELECT
      id,
      name
    FROM
      product
    WHERE
      @Names::text[] IS NULL OR name = ANY(@Names);",
  new
  {
      Names = Names,
  });