主頁 > 知識庫 > Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例

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

熱門標(biāo)簽:怎么給超市做地圖標(biāo)注入駐店 外呼系統(tǒng)代理品牌 手機(jī)地圖標(biāo)注如何刪除 巫師3為什么地圖標(biāo)注的財寶沒有 世紀(jì)佳緣地圖標(biāo)注怎么去掉 辦理400電話證件 寧波自動外呼系統(tǒng)代理 十堰正規(guī)電銷機(jī)器人系統(tǒng) 外呼系統(tǒng)費(fèi)用一年

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"}]

代碼如下:

復(fù)制代碼 代碼如下:

public void InsertOrUpdateOnlyItem(ListtblLims_Ana_LE_Import_Common> listLe)
        {
            var listLeInsert = new ListtblLims_Ana_LE_Import_Common>();
            var listLeUpdate = new ListtblLims_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.DeserializeListDictionarystring, string>>>(own.fldImportData);  //原數(shù)據(jù)
                    var listmodel = ser.DeserializeListDictionarystring, string>>>(model.fldImportData); //新數(shù)據(jù)
                    IEqualityComparerDictionarystring, 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() 方法中用到的自定義比較類:

復(fù)制代碼 代碼如下:

/// summary>
    /// 自定義比較類
    /// /summary>
    public class EntityComparer : IEqualityComparerDictionarystring, string>>
    {
        public bool Equals(Dictionarystring, string> x, Dictionarystring, 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(Dictionarystring, 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;
        }
    }

您可能感興趣的文章:
  • JS實(shí)現(xiàn)合并json對象的方法
  • JavaScript簡單實(shí)現(xiàn)合并兩個Json對象的方法示例
  • JavaScript實(shí)現(xiàn)JSON合并操作示例【遞歸深度合并】
  • js根據(jù)json數(shù)據(jù)中的某一個屬性來給數(shù)據(jù)分組的方法
  • Javascript中JSON數(shù)據(jù)分組優(yōu)化實(shí)踐及JS操作JSON總結(jié)
  • JS遍歷JSON數(shù)組及獲取JSON數(shù)組長度操作示例【測試可用】
  • JavaScript實(shí)現(xiàn)構(gòu)造json數(shù)組的方法分析
  • JS實(shí)現(xiàn)鍵值對遍歷json數(shù)組功能示例
  • JavaScript數(shù)組,JSON對象實(shí)現(xiàn)動態(tài)添加、修改、刪除功能示例
  • js實(shí)現(xiàn)json數(shù)組分組合并操作示例

標(biāo)簽:景德鎮(zhèn) 嘉興 牡丹江 泰州 山西 通遼 天門

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例》,本文關(guān)鍵詞  Queryable.Union,方法,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例》相關(guān)的同類信息!
  • 本頁收集關(guān)于Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章