91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用Queryable.Union方法實現json格式的字符串合并

發布時間:2021-10-09 09:40:03 來源:億速云 閱讀:115 作者:iii 欄目:開發技術

本篇內容主要講解“如何用Queryable.Union方法實現json格式的字符串合并”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何用Queryable.Union方法實現json格式的字符串合并”吧!

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

到此,相信大家對“如何用Queryable.Union方法實現json格式的字符串合并”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

梅州市| 江口县| 乌兰县| 景谷| 闻喜县| 榕江县| 威远县| 卢湾区| 布尔津县| 白水县| 高州市| 扶余县| 文水县| 哈密市| 通州市| 敦煌市| 左云县| 胶州市| 洛扎县| 秀山| 喀喇沁旗| 西平县| 南雄市| 潼关县| 渭南市| 曲松县| 巫溪县| 祁东县| 高青县| 鸡东县| 温宿县| 高陵县| 沂水县| 绥棱县| 威信县| 天峨县| 怀远县| 新宾| 郴州市| 固安县| 黄大仙区|