Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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#';Postgres表中的s位数组_C#_Postgresql_Bitarray - Fatal编程技术网

如何正确存储C#';Postgres表中的s位数组

如何正确存储C#';Postgres表中的s位数组,c#,postgresql,bitarray,C#,Postgresql,Bitarray,目前,我正在尝试在Postgres表中存储可变长度的位模式。我的用例是,我想以一种紧凑的方式对分配给组的数据记录的一些信息进行编码 Postgres中我的表的简化模式如下所示: CREATE TABLE axivas.group_records ( id int4 NOT NULL GENERATED ALWAYS AS IDENTITY, record_id int4 NOT NULL, group_ids varbit(50) NOT NULL, CONSTRAINT group_recor

目前,我正在尝试在Postgres表中存储可变长度的位模式。我的用例是,我想以一种紧凑的方式对分配给组的数据记录的一些信息进行编码

Postgres中我的表的简化模式如下所示:

CREATE TABLE axivas.group_records (
id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
record_id int4 NOT NULL,
group_ids varbit(50) NOT NULL,
CONSTRAINT group_records_pkey PRIMARY KEY (id));
try
{
     var context = new xerxesdevtestsContext();

     Random rnd = new Random();

     for (int i = 0; i < 1024; i++)
     {

        BitArray ba = new BitArray(rnd.Next(10, 50));
        ba.SetAll(false);
        for (int j=rnd.Next(0,5);j<rnd.Next(5,ba.Length-1);j++)
        {
            ba[j] = true;
        }
        context.GroupRecords.Add(new GroupRecords()
        {
            GroupIds = ba,
            RecordId = i
        });

     }
     context.SaveChanges();
 }
 catch (Exception ex)
 {
     Console.WriteLine("Error: " + ex.Message);
 }
BitArray ba = new BitArray(rnd.Next(10, 50));

ba.SetAll(false);
ba[0] = true;

for (int j = 1; j < ba.Length - 1; j++)
{
    ba[j] = Convert.ToBoolean(rnd.Next(2));
}
在C#应用程序中,我使用Npgsql实体框架核心创建实体,如下所示:

CREATE TABLE axivas.group_records (
id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
record_id int4 NOT NULL,
group_ids varbit(50) NOT NULL,
CONSTRAINT group_records_pkey PRIMARY KEY (id));
try
{
     var context = new xerxesdevtestsContext();

     Random rnd = new Random();

     for (int i = 0; i < 1024; i++)
     {

        BitArray ba = new BitArray(rnd.Next(10, 50));
        ba.SetAll(false);
        for (int j=rnd.Next(0,5);j<rnd.Next(5,ba.Length-1);j++)
        {
            ba[j] = true;
        }
        context.GroupRecords.Add(new GroupRecords()
        {
            GroupIds = ba,
            RecordId = i
        });

     }
     context.SaveChanges();
 }
 catch (Exception ex)
 {
     Console.WriteLine("Error: " + ex.Message);
 }
BitArray ba = new BitArray(rnd.Next(10, 50));

ba.SetAll(false);
ba[0] = true;

for (int j = 1; j < ba.Length - 1; j++)
{
    ba[j] = Convert.ToBoolean(rnd.Next(2));
}
试试看
{
var context=new xerxesdevtestsContext();
随机rnd=新随机();
对于(int i=0;i<1024;i++)
{
BitArray ba=新的位数组(rnd.Next(10,50));
ba.SetAll(假);;

对于(int j=rnd.Next(0,5);j很明显,位数组具有前导零的原因是因为在
for
循环中,
j
被初始化为一些很少为零的值。
j
必须始终为零才能开始在数组的开头放置1。否则,在大多数情况下,您将以0结尾前导零

因此,如果您想要可变长度的随机二进制数,为什么不这样做:

CREATE TABLE axivas.group_records (
id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
record_id int4 NOT NULL,
group_ids varbit(50) NOT NULL,
CONSTRAINT group_records_pkey PRIMARY KEY (id));
try
{
     var context = new xerxesdevtestsContext();

     Random rnd = new Random();

     for (int i = 0; i < 1024; i++)
     {

        BitArray ba = new BitArray(rnd.Next(10, 50));
        ba.SetAll(false);
        for (int j=rnd.Next(0,5);j<rnd.Next(5,ba.Length-1);j++)
        {
            ba[j] = true;
        }
        context.GroupRecords.Add(new GroupRecords()
        {
            GroupIds = ba,
            RecordId = i
        });

     }
     context.SaveChanges();
 }
 catch (Exception ex)
 {
     Console.WriteLine("Error: " + ex.Message);
 }
BitArray ba = new BitArray(rnd.Next(10, 50));

ba.SetAll(false);
ba[0] = true;

for (int j = 1; j < ba.Length - 1; j++)
{
    ba[j] = Convert.ToBoolean(rnd.Next(2));
}

是的,对于问题的C#端,这是显而易见的。实际上,在C#端,前导零的生成是为了能够检查Postgres如何存储位字符串。也许我应该更精确。我想知道PostgreSQL为什么存储前导零。它们不携带任何有价值的信息,所以我的理解是,它们是not存储,当varbit用作列中的数据类型时。但是,似乎任何指定的位都将被存储,并且varbit字段的长度只定义了最大长度。我的理解正确吗?@NightFlyer35是的,这让人困惑。你真正的问题可能很简单为什么PostgreSQL在varbit列中存储前导零。"你本可以隐藏C#涉及的事实,只是展示了几个例子,其中你手动将二进制序列插入数据库。在回答这个问题时,
varbit
并没有声称是二进制数表示,它只是声称持有位序列。我相信我们会合法化在某些情况下,我需要前导零。很抱歉给@robbpriestley带来了混乱。记住您的评论,我使用一个小助手类解决了我的问题,该类负责正确的位字段长度。