Postgres integer arrays as parameters?

前端 未结 3 927
灰色年华
灰色年华 2020-12-13 02:13

I understand that in Postgres pure, you can pass an integer array into a function but that this isn\'t supported in the .NET data provider Npgsql.

I currently have

3条回答
  •  一生所求
    2020-12-13 02:39

    You can always use a properly formatted string. The trick is the formatting.

    command.Parameters.Add("@array_parameter", string.Format("{{{0}}}", string.Join(",", array));

    Note that if your array is an array of strings, then you'll need to use array.Select(value => string.Format("\"{0}\", value)) or the equivalent. I use this style for an array of an enumerated type in PostgreSQL, because there's no automatic conversion from the array.

    In my case, my enumerated type has some values like 'value1', 'value2', 'value3', and my C# enumeration has matching values. In my case, the final SQL query ends up looking something like (E'{"value1","value2"}'), and this works.

提交回复
热议问题