2

我有一张名为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 而过载。

你能告诉我如何实现这个吗?

4

1 回答 1

0

已更新此更新添加了更多错误检查,并假定 -1 永远不是行业 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();
于 2012-10-11T10:27:24.830 回答