Queryable.Union 方法實現(xiàn)json格式的字符串合并的具體實例
1.在數(shù)據(jù)庫中以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ù)替換原來的數(shù)據(jù)。
如:數(shù)據(jù)庫中原保存的數(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(); //自定義的比較類
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ù)庫中存數(shù)據(jù)的表
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;
}
}
- JS實現(xiàn)合并json對象的方法
- JavaScript簡單實現(xiàn)合并兩個Json對象的方法示例
- JavaScript實現(xiàn)JSON合并操作示例【遞歸深度合并】
- js根據(jù)json數(shù)據(jù)中的某一個屬性來給數(shù)據(jù)分組的方法
- Javascript中JSON數(shù)據(jù)分組優(yōu)化實踐及JS操作JSON總結(jié)
- JS遍歷JSON數(shù)組及獲取JSON數(shù)組長度操作示例【測試可用】
- JavaScript實現(xiàn)構(gòu)造json數(shù)組的方法分析
- JS實現(xiàn)鍵值對遍歷json數(shù)組功能示例
- JavaScript數(shù)組,JSON對象實現(xiàn)動態(tài)添加、修改、刪除功能示例
- js實現(xiàn)json數(shù)組分組合并操作示例
相關(guān)文章
ASP.NET從字符串中查找字符出現(xiàn)次數(shù)的具體實現(xiàn)方法
今天在一場“特殊的討論”中引入了一個問題,如何在C#求出字符串中某字符的出現(xiàn)次數(shù),比如求“ADSFGEHERGASDF”中“A”出現(xiàn)的次數(shù)2013-11-11asp.net 計劃任務(wù)管理程序?qū)崿F(xiàn),多線程任務(wù)加載
b/s模式下用程序?qū)崿F(xiàn)計劃任務(wù),一直是個不太好解決和管理的問題,當然可以采用ajax 計時器的方法模擬form端的timer事件。2009-11-11