我有一个名为industries
的表。在这里我的领域是
workfor_id
,workfor_usr_id
,workfor_industry_id
。使用相同的workfor_id
值,我有不同的workfor_industry_id
。
foreach (var k in us){
var ind = dbContext.industries.Where(i => i.workfor_id ==
k.id).Select(i => i).FirstOrDefault();
string ind2 = k.industry;
var industryParts = ind2.Split(',');
var o = (industryParts.Length);
for (c = 0; c < o; c++){
ind.workfor_id = Convert.ToInt16(k.id);
ind.workfor_industry_id = Convert.ToInt16(k.industryid); }
}
要更新workfor_industry_id字段,我在foreach循环中实现了内部循环,以获取workfor_industry_id的值。其中相同的记录超载了不同的workfor_industry_id。
你能告诉我如何实现这个。
答案 0 :(得分:0)
已更新 此更新添加了更多错误检查,并假定-1永远不是industry_id
的有效值short GetShort(string value) {
short returnValue;
value = (value ?? string.Empty).Replace("\"",null);
return short.TryParse(value, out returnValue) ? returnValue : (short)-1;
}
foreach (var k in us){
var id=Convert.ToInt16(k.id);
var toRemove=from i in dbContext.industries
where i.workfor_id == k.id
select i;
var toAdd = from x in (k.industry ?? string.Empty).Split(',')
select new Industry {
workfor_id=id,
workfor_industry_id=GetShort(x)
};
dbContext.industries.DeleteAllOnSubmit(toRemove);
dbContext.industries.InsertAllOnSubmit(toAdd.Where(x=>x.workfor_industry_id != -1));
}
dbContext.SubmitChanges();