Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例
1.在數(shù)據(jù)庫(kù)中以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ù)替換原來(lái)的數(shù)據(jù)。
如:數(shù)據(jù)庫(kù)中原保存的數(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"}]
代碼如下:
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); //原數(shù)據(jù)
var listmodel = ser.Deserialize<List<Dictionary<string, string>>>(model.fldImportData); //新數(shù)據(jù)
IEqualityComparer<Dictionary<string, string>> ec = new EntityComparer(); //自定義的比較類(lèi)
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ù)庫(kù)中存數(shù)據(jù)的表
Union() 方法中用到的自定義比較類(lèi):
/// <summary>
/// 自定義比較類(lèi)
/// </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"]; //如果名稱(chēng)相同就不追加
}
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;
}
}
- JS實(shí)現(xiàn)合并json對(duì)象的方法
- JavaScript簡(jiǎn)單實(shí)現(xiàn)合并兩個(gè)Json對(duì)象的方法示例
- JavaScript實(shí)現(xiàn)JSON合并操作示例【遞歸深度合并】
- js根據(jù)json數(shù)據(jù)中的某一個(gè)屬性來(lái)給數(shù)據(jù)分組的方法
- Javascript中JSON數(shù)據(jù)分組優(yōu)化實(shí)踐及JS操作JSON總結(jié)
- JS遍歷JSON數(shù)組及獲取JSON數(shù)組長(zhǎng)度操作示例【測(cè)試可用】
- JavaScript實(shí)現(xiàn)構(gòu)造json數(shù)組的方法分析
- JS實(shí)現(xiàn)鍵值對(duì)遍歷json數(shù)組功能示例
- JavaScript數(shù)組,JSON對(duì)象實(shí)現(xiàn)動(dòng)態(tài)添加、修改、刪除功能示例
- js實(shí)現(xiàn)json數(shù)組分組合并操作示例
相關(guān)文章
.NET Core 2.0如何生成圖片驗(yàn)證碼完整實(shí)例
這篇文章主要給大家介紹了關(guān)于.NET Core 2.0如何生成圖片驗(yàn)證碼的相關(guān)資料,該功能主要是利用ZKWeb.System.Drawing來(lái)實(shí)現(xiàn),文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
.NET?Core使用SkiaSharp實(shí)現(xiàn)快速生成二維碼
這篇文章主要為大家詳細(xì)介紹了.NET?Core如何使用SkiaSharp實(shí)現(xiàn)快速生成二維碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
無(wú)法將函數(shù)定義與現(xiàn)有的聲明匹配 問(wèn)題的解決辦法 分享
無(wú)法將函數(shù)定義與現(xiàn)有的聲明匹配 問(wèn)題的解決辦法 分享,需要的朋友可以參考一下2013-05-05
.net core下配置訪問(wèn)數(shù)據(jù)庫(kù)操作
本篇文章給大家詳細(xì)分享了在.net core下配置訪問(wèn)數(shù)據(jù)庫(kù)的相關(guān)操作過(guò)程以及代碼實(shí)現(xiàn)過(guò)程,有興趣的朋友參考下。2018-03-03
ASP.net在頁(yè)面所有內(nèi)容生成后、輸出內(nèi)容前對(duì)頁(yè)面內(nèi)容進(jìn)行操作
ASP.net在頁(yè)面所有內(nèi)容生成后、輸出內(nèi)容前對(duì)頁(yè)面內(nèi)容進(jìn)行操作...2007-04-04
asp.net小孔子cms中的數(shù)據(jù)添加修改
最近都在看小孔子cms的代碼,其添加與修改數(shù)據(jù)十分方便,做下筆記,代碼主要提取自小孔子cms,去掉了不用的函數(shù)并把相關(guān)代碼寫(xiě)到一個(gè)文件中2008-08-08
關(guān)于Net6?Xunit?集成測(cè)試的問(wèn)題
這篇文章主要介紹了Net6?Xunit?集成測(cè)試的相關(guān)知識(shí),下面我將Net6下沒(méi)有使用Startup以及NET6以前版本使用Startup的集成測(cè)試(單元測(cè)試?yán)淄┳鲆粋€(gè)梳理,需要的朋友可以參考下2022-05-05

