1

我正在使用 LINQ to SQL,我有一个名为“tblCourse”的表,我想根据某些条件从该表中选择一些行,然后将这些选定的行作为新记录插入到同一个表中。任何机构都可以提出一些帖子、链接或方向吗?

public IQueryable<tblCourseNeededHours> CloneAllCourseNeededHoursInDepartmentInSemester(int departmentId, string semester)
{
return from allCourses in _db.tblCourseNeededHours orderby allCourses.tblCourse.CourseName
where allCourses.tblCourse.tblInstitute.DepartmentID == departmentId && allCourses.Semester
== semester select allCourses;

var thisTable = from t in _db.tblCourseNeededHours where
t.tblCourse.tblInstitute.DepartmentID == departmentId && t.Semester == semester

//select everything except ID
select _db.tblCourseNeededHours {  Semester = ?, NeedHoursPerWeek = ?, AssignToInstitute=? 
(I want it null for new record), Freezed=? (I want it null for new record)    
}; 
_db.tblCourseNeededHours.InsertAllOnsubmit(thisTable);
_db.SubmitChanges(); 

现在当我使用

select _db.tblCourseNeededHours {  Semester = ?, NeedHoursPerWeek = ?, AssignToInstitute=? (I want it null), Freezed=? (I want it null)    
};

? 提到“我应​​该把什么放在这里,我在这里有错误,也在这条线上

_db.tblCourseNeededHours.InsertAllOnsubmit(thisTable);
4

1 回答 1

0

我不确定您的数据库架构,但是这样的东西应该适合您。

var oldItems= (from p on db.tblCourses where p.InstructorId=43 select p).ToList();
if(oldItems!=null)
{
  foreach(var item in oldItems)
  {
    tblCourse objNew=new tblCourse();
    objNew.Instructor="John";  
   // update someproperty values from old row if you need        
    objNew.Credits=item.Credits+2;
    db.tblCourses.Add(objNew);
  }
  db.SubmitChanges();
}

假设 db 是您的数据上下文

于 2012-04-08T18:57:46.503 回答