Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report