The Queryable. Union method implements a concrete instance of string merging in json format

  • 2020-07-21 07:24:22
  • OfStack

1. In the database to json string format, such as: [{" name ":" 3 ", "time" : "8.592", "area" : "27.27033", "conc" : "4.12136"}, {" name ":" 4 "lee," time ":" 9.100 ", "area" : "56.21229", "conc" : "4.57692"}]

2. Merge different data after adding new content. If name is the same, replace the original data with the latest data.

Zhongyuan saved data such as database is [{" name ":" 3 ", "time" : "8.592", "area" : "27.27033", "conc" : "4.12136"}, {" name ":" 4 "lee," time ":" 9.100 ", "area" : "56.21229", "conc" : "4.57692"}]

New data for [{" name ":" 3 ", "time" : "12", "area" : "27.70533", "conc" : "4.12136"}, {" name ":" 5 "king," time ":" 4 ", "area" : "77", "conc" : "8.788"}]

Replace the data of the [{" name ":" 3 ", "time" : "12", "area" : "27.70533", "conc" : "4.12136"}, {" name ":" 5 "king," time ":" 4 ", "area" : "77", "conc" : "8.788"}, {" name ":" 4 "lee," time ":" 9.100 ", "area" : "56.21229", "conc" :" 4.57692 "}]

The code is as follows:


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);  // The original data 
                    var listmodel = ser.Deserialize<List<Dictionary<string, string>>>(model.fldImportData); // The new data 
                    IEqualityComparer<Dictionary<string, string>> ec = new EntityComparer();   // Custom comparison classes 
                    own.fldImportData = ser.Serialize(listmodel.Union(listown, ec));  // Merge data 

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

tblLims_Ana_LE_Import_Common is the table in which data is stored in the database

The custom comparison class used in the Union() method:


/// <summary> 
    ///  Custom comparison classes  
    /// </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"];  // If the name is the same, it is not appended 
        }
        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;
        }
    }


Related articles: