最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Queryable.Union 方法實現(xiàn)json格式的字符串合并的具體實例

 更新時間:2013年10月14日 14:42:40   作者:  
這篇文章介紹了Queryable.Union 方法實現(xiàn)json格式的字符串合并的具體實例,有需要的朋友可以參考一下

1.在數(shù)據(jù)庫中以json字符串格式保存,如:[{"name":"張三","time":"8.592","area":"27.27033","conc":"4.12136"},{"name":"李四","time":"9.100","area":"56.21229","conc":"4.57692"}]

2.添加新內(nèi)容后合并不相同的數(shù)據(jù)。如果name相同,以最新的數(shù)據(jù)替換原來的數(shù)據(jù)。

如:數(shù)據(jù)庫中原保存的數(shù)據(jù)是[{"name":"張三","time":"8.592","area":"27.27033","conc":"4.12136"},{"name":"李四","time":"9.100","area":"56.21229","conc":"4.57692"}]

新加的數(shù)據(jù)為[{"name":"張三","time":"12","area":"27.70533","conc":"4.12136"},{"name":"王五","time":"4","area":"77","conc":"8.788"}]

 則替換后的數(shù)據(jù)為[{"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);  //原數(shù)據(jù)
                    var listmodel = ser.Deserialize<List<Dictionary<string, string>>>(model.fldImportData); //新數(shù)據(jù)
                    IEqualityComparer<Dictionary<string, string>> ec = new EntityComparer();   //自定義的比較類
                    own.fldImportData = ser.Serialize(listmodel.Union(listown, ec));  //合并數(shù)據(jù)


                    listLeUpdate.Add(own);
                }
                else
                {
                    listLeInsert.Add(model);
                }
            }
            CurrentRepository.UpdateAll(listLeUpdate);
            CurrentRepository.InsertAll(listLeInsert);
            CurrentRepository.Save();
        }


tblLims_Ana_LE_Import_Common 為數(shù)據(jù)庫中存數(shù)據(jù)的表

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;
        }
    }

相關(guān)文章

最新評論

恩施市| 绥滨县| 东光县| 三穗县| 禄劝| 河源市| 周口市| 新巴尔虎左旗| 绥芬河市| 佛学| 建湖县| 北流市| 中山市| 迭部县| 绩溪县| 类乌齐县| 麻阳| 北安市| 策勒县| 乐安县| 麻江县| 阳春市| 阜新市| 江山市| 三江| 蚌埠市| 屯昌县| 虹口区| 中宁县| 肇东市| 武胜县| 麻阳| 杨浦区| 南城县| 宜春市| 个旧市| 东阳市| 于都县| 姜堰市| 阳信县| 乌恰县|