dv.ToTable()datatable去重方法BUG求助

狂舖 2019-08-23 09:33:12
最近做了一个项目有这样一个需求:需要扫描产品的二维码,根据二维码内容来纪录和展示数据,但是展示的数据要保证唯一性(作业员有重复扫描同一张二维码的情况),所以就在网上查到了如下方法,对数据进行去重复处理。但是实际作业中发现在执行这个方法时会有如下问题存在:第(N+1)次扫描的数据,去重后会变成第(N)次扫描的数据。日志记录如下:
其中:“D20599087559144.5-006”为第(N+1)次扫描的数据信息,但是去重后,会得到“D20599037074188.5-005”。不知道自己的代码为什么会出现这种BUG。
奇怪的是,这种情况不是经常出现,而是偶尔会出现那么一次两次。所以很奇怪。

附代码如下:
/*
datatable 为要去重的DataTable数据源
dt1为去重后的DataTable
qjarr为要显示的列的数组,这个地方列的顺序与datatable中列的顺序不太一致

*/
DataView dv = new DataView(dataTable);
DataTable tempdatatable = new DataTable();//创建一个临时table来做为dataset的去重后的table
tempdatatable = dv.ToTable(true, qjarr);//得到去重后的datatable
DataRow newrow = dt1.NewRow(); ;//newrow就是dt1的新行
newrow = tempdatatable.Rows[tempdatatable.Rows.Count - 1];//将值给newrow
dt1.Rows.Add(newrow.ItemArray);//把值填入dt1
//后面这两条代码是自己写的添加日志的方法
CompoundLogWrite("dt1:"+dt1.Rows.Count.ToString(), "dataTable:" + dataTable.Rows.Count.ToString(), "dataTable");//日志记录datatable长度
CompoundLogWrite("dt1-1T:" + dt1.Rows[dt1.Rows.Count-1]["1T"].ToString(), "dataTable-1T:" + dataTable.Rows[dataTable.Rows.Count - 1]["1T"].ToString(), "1T");//记录最后录入的批次信息
...全文
268 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
狂舖 2019-08-26
  • 打赏
  • 举报
回复
最近论坛怎么这么不活跃?人呢?
狂舖 2019-08-23
  • 打赏
  • 举报
回复
引用 2 楼 赵4老师 的回复:
仅供参考:
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128 //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
if (argc<3) {
printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
return 1;
}
if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
f=fopen(argv[1],"r");
if (NULL==f) {
printf("Can not find file %s!\n",argv[1]);
return 1;
}
buf=(char *)malloc(MAXLINES*MAXCHARS);
if (NULL==buf) {
fclose(f);
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
return 2;
}
n=0;
hh=0;
i=0;
while (1) {
if (NULL==fgets(ln,MAXCHARS,f)) break;//
hh++;
L=strlen(ln)-1;
if ('\n'!=ln[L]) {//超长行忽略后面内容
printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
while (1) {
c=fgetc(f);
if ('\n'==c || EOF==c) break;//
}
}
while (1) {//去掉行尾的'\n'和空格
if ('\n'==ln[L] || ' '==ln[L]) {
ln[L]=0;
L--;
if (L<0) break;//
} else break;//
}
if (L>=0) {
strcpy(buf+i,ln);i+=MAXCHARS;
n++;
if (n>=MAXLINES) {
MAXLINES2=MAXLINES*2;
if (MAXLINES2==1280000) MAXLINES2=2500000;
buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
if (NULL==buf2) {
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
printf("WARNING: Lines >%d ignored.\n",MAXLINES);
break;//
}
buf=buf2;
MAXLINES=MAXLINES2;
}
}
}
fclose(f);
if (n>1) {
if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
else qsort(buf,n,MAXCHARS,compare);
}
f=fopen(argv[2],"w");
if (NULL==f) {
free(buf);
printf("Can not create file %s!\n",argv[2]);
return 2;
}
fprintf(f,"%s\n",buf);
if (n>1) {
if (ignore_case) {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
} else {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
}
}
fclose(f);
free(buf);
return 0;
}
首先感谢回帖,从中得到启发,现在正在尝试将数据放入数组中进行处理。方法写完后会将测试结果分享出来
赵4老师 2019-08-23
  • 打赏
  • 举报
回复
仅供参考:
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128      //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
   return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
   return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
    if (argc<3) {
        printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
        printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
        return 1;
    }
    if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
    f=fopen(argv[1],"r");
    if (NULL==f) {
        printf("Can not find file %s!\n",argv[1]);
        return 1;
    }
    buf=(char *)malloc(MAXLINES*MAXCHARS);
    if (NULL==buf) {
        fclose(f);
        printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
        return 2;
    }
    n=0;
    hh=0;
    i=0;
    while (1) {
        if (NULL==fgets(ln,MAXCHARS,f)) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
            while (1) {
                c=fgetc(f);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            strcpy(buf+i,ln);i+=MAXCHARS;
            n++;
            if (n>=MAXLINES) {
                MAXLINES2=MAXLINES*2;
                if (MAXLINES2==1280000) MAXLINES2=2500000;
                buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
                if (NULL==buf2) {
                    printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
                    printf("WARNING: Lines >%d ignored.\n",MAXLINES);
                    break;//
                }
                buf=buf2;
                MAXLINES=MAXLINES2;
            }
        }
    }
    fclose(f);
    if (n>1) {
        if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
        else qsort(buf,n,MAXCHARS,compare);
    }
    f=fopen(argv[2],"w");
    if (NULL==f) {
        free(buf);
        printf("Can not create file %s!\n",argv[2]);
        return 2;
    }
    fprintf(f,"%s\n",buf);
    if (n>1) {
        if (ignore_case) {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        } else {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        }
    }
    fclose(f);
    free(buf);
    return 0;
}
狂舖 2019-08-23
  • 打赏
  • 举报
回复
其中“D20599037074188.5-005”为第N次录入的数据信息

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧