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"}]
代碼如下:
復(fù)制代碼 代碼如下:
public void InsertOrUpdateOnlyItem(ListtblLims_Ana_LE_Import_Common> listLe)
{
var listLeInsert = new ListtblLims_Ana_LE_Import_Common>();
var listLeUpdate = new ListtblLims_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.DeserializeListDictionarystring, string>>>(own.fldImportData); //原數(shù)據(jù)
var listmodel = ser.DeserializeListDictionarystring, string>>>(model.fldImportData); //新數(shù)據(jù)
IEqualityComparerDictionarystring, 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() 方法中用到的自定義比較類:
復(fù)制代碼 代碼如下:
/// summary>
/// 自定義比較類
/// /summary>
public class EntityComparer : IEqualityComparerDictionarystring, string>>
{
public bool Equals(Dictionarystring, string> x, Dictionarystring, 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(Dictionarystring, 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對象的方法
- JavaScript簡單實(shí)現(xiàn)合并兩個Json對象的方法示例
- JavaScript實(shí)現(xiàn)JSON合并操作示例【遞歸深度合并】
- js根據(jù)json數(shù)據(jù)中的某一個屬性來給數(shù)據(jù)分組的方法
- Javascript中JSON數(shù)據(jù)分組優(yōu)化實(shí)踐及JS操作JSON總結(jié)
- JS遍歷JSON數(shù)組及獲取JSON數(shù)組長度操作示例【測試可用】
- JavaScript實(shí)現(xiàn)構(gòu)造json數(shù)組的方法分析
- JS實(shí)現(xiàn)鍵值對遍歷json數(shù)組功能示例
- JavaScript數(shù)組,JSON對象實(shí)現(xiàn)動態(tài)添加、修改、刪除功能示例
- js實(shí)現(xiàn)json數(shù)組分組合并操作示例