Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Queryable.Union method to realize string merging in json format

Shulou Source: shulou.com Published: 2022-06-03 07:38:12 10月04日 Update

This article focuses on "how to use the Queryable.Union method to achieve string merging in json format", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the Queryable.Union method to achieve string merging in json format.

1. Save in the database in json string format, such as [{"name": "Zhang San", "time": "8.592", "area": "27.27033", "conc": "4.12136"}, {"name": "Li Si", "time": "9.100", "area": "56.21229", "conc": "4.57692"}]

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

For example, the data stored in the Central Plains of the database are [{"name": "Zhang San", "time": "8.592", "area": "27.27033", "conc": "4.12136"}, {"name": "Li Si", "time": "9.100", "area": "56.21229", "conc": "4.57692"}]

The new data are [{"name": "Zhang San", "time": "12", "area": "27.70533", "conc": "4.12136"}, {"name": "Wang Wu", "time": "4", "area": "77", "conc": "8.788"}]

The replaced data are [{"name": "Zhang San", "time": "12", "area": "27.70533", "conc": "4.12136"}, {"name": "Wang Wu", "time": "4", "area": "77", "conc": "8.788"}, {"name": "Li Si", "time": "9.100", "area": "56.21229", "conc": "4.57692"}]

The copy code is as follows:

Public void InsertOrUpdateOnlyItem (List listLe)

{

Var listLeInsert = new List ()

Var listLeUpdate = new List ()

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 (own.fldImportData); / / original data

Var listmodel = ser.Deserialize (model.fldImportData); / / New data

IEqualityComparer ec = new EntityComparer (); / / Custom comparison class

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 that stores data in the database.

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

The copy code is as follows:

/ / /

/ / Custom comparison class

/ / /

Public class EntityComparer: IEqualityComparer

{

Public bool Equals (Dictionary x, Dictionary y)

{

If (ReferenceEquals (x, y)) return true

If (ReferenceEquals (x, null) | | ReferenceEquals (y, null))

Return false

Return x ["name"] = y ["name"]; / / do not append if the name is the same

}

Public int GetHashCode (Dictionary 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

}

}

At this point, I believe you have a deeper understanding of "how to use the Queryable.Union method to achieve string merging in json format". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data method character string format code Zhang San same database Li Si content Wang Wu learning practical deeper Chinese use interest name practicality practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information Xiaomi NVidia macOS