欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Queryable.Union 方法实现json格式的字符串合并的具体实例

程序员文章站 2024-02-27 22:25:15
1.在数据库中以json字符串格式保存,如:[{"name":"张三","time":"8.592","area":"27.27033","conc":"4.12136"}...

1.在数据库中以json字符串格式保存,如:[{"name":"张三","time":"8.592","area":"27.27033","conc":"4.12136"},{"name":"李四","time":"9.100","area":"56.21229","conc":"4.57692"}]

2.添加新内容后合并不相同的数据。如果name相同,以最新的数据替换原来的数据。

如:数据库中原保存的数据是[{"name":"张三","time":"8.592","area":"27.27033","conc":"4.12136"},{"name":"李四","time":"9.100","area":"56.21229","conc":"4.57692"}]

新加的数据为[{"name":"张三","time":"12","area":"27.70533","conc":"4.12136"},{"name":"王五","time":"4","area":"77","conc":"8.788"}]

 则替换后的数据为[{"name":"张三","time":"12","area":"27.70533","conc":"4.12136"},{"name":"王五","time":"4","area":"77","conc":"8.788"},{"name":"李四","time":"9.100","area":"56.21229","conc":"4.57692"}]

代码如下:

复制代码 代码如下:

public void insertorupdateonlyitem(list<tbllims_ana_le_import_common> listle)
        {
            var listleinsert = new list<tbllims_ana_le_import_common>();
            var listleupdate = new list<tbllims_ana_le_import_common>();
            foreach (var le in listle)
            {
                tbllims_ana_le_import_common model = le;
                var own = currentrepository.find(a => a.fldtaskid == model.fldtaskid
                && a.fldbizcatid == model.fldbizcatid
                && a.flditemcode == model.flditemcode
                && a.fldnumber == model.fldnumber
                && a.fldsamplecode == model.fldsamplecode);
                if (own != null)
                {
                    var ser = new javascriptserializer();

                    var listown = ser.deserialize<list<dictionary<string, string>>>(own.fldimportdata);  //原数据
                    var listmodel = ser.deserialize<list<dictionary<string, string>>>(model.fldimportdata); //新数据
                    iequalitycomparer<dictionary<string, string>> ec = new entitycomparer();   //自定义的比较类
                    own.fldimportdata = ser.serialize(listmodel.union(listown, ec));  //合并数据


                    listleupdate.add(own);
                }
                else
                {
                    listleinsert.add(model);
                }
            }
            currentrepository.updateall(listleupdate);
            currentrepository.insertall(listleinsert);
            currentrepository.save();
        }


tbllims_ana_le_import_common 为数据库中存数据的表

union() 方法中用到的自定义比较类:

复制代码 代码如下:

/// <summary>
    /// 自定义比较类
    /// </summary>
    public class entitycomparer : iequalitycomparer<dictionary<string, string>>
    {
        public bool equals(dictionary<string, string> x, dictionary<string, string> y)
        {
            if (referenceequals(x, y)) return true;

            if (referenceequals(x, null) || referenceequals(y, null))
                return false;

            return x["name"] == y["name"];  //如果名称相同就不追加
        }

        public int gethashcode(dictionary<string, string> obj)
        {
            if (referenceequals(obj, null)) return 0;
            int hashname = obj["name"] == null ? 0 : obj["name"].gethashcode();
            int hashcode = obj["name"] == null ? 0 : obj["name"].gethashcode();
            return hashname ^ hashcode;
        }
    }