C# 更新集合中某一个信息也是集合的数据
比如一个集合中,还有一个多选项,此多选项是作为一个集合,传输在总的集合上,更新这个多选项集合的代码
1.先更新总集合的基本数据
2.针对这个多选项集合的值,更新多选项
1 //从DB删除已经删除的问题 2 if (exam.ID > 0) 3 { 4 var sql = string.Empty; 5 if (exam.Questions.Where(o => o.ID > 0).Count() > 0) 6 { 7 sql = string.Format("UPDATE dbo.Question SET IsDeleted = 1 WHERE IsDeleted = 0 AND ExamSn = '{0}' AND ID NOT IN ({1})", exam.Sn, string.Join(",", exam.Questions.Where(o => o.ID > 0).Select(o => o.ID))); 8 } 9 else 10 { 11 sql = string.Format("UPDATE dbo.Question SET IsDeleted = 1 WHERE IsDeleted = 0 AND ExamSn = '{0}'", exam.Sn); 12 } 13 db.ExecuteNonQuerySql(sql); 14 } 15 foreach (var q in exam.Questions) 16 { 17 var q_result = false; 18 if (q.ID == 0) 19 { 20 q.IsDeleted = false; 21 q.Sn = Guid.NewGuid(); 22 q.ExamSn = exam.Sn; 23 q_result = service.Create(q); 24 } 25 else 26 { 27 q_result = service.Update(q); 28 }30 }