程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> Queryable.Union 方法實現json格式的字符串合並的具體實例

Queryable.Union 方法實現json格式的字符串合並的具體實例

編輯:ASP.NET基礎

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved