0

나는 다음과 같은 코드가 있습니다이 코드가 테이블에서 행을 삭제하지 않는 이유는 무엇입니까?

Using dbContext As pbu_housingEntities = New pbu_housingEntities 
     ' First, delete all current records associated with the specified semester. 
     Dim delete_old = (From p In dbContext.Residents _ 
          Where p.semester = txtDestSemester.Text _ 
          Where p.year = txtDestYear.Text _ 
          Select p) 
     dbContext.Residents.DeleteObject(delete_old) 
     dbContext.SaveChanges() 
End Using 

을하지만 그것은 작동하지 않습니다. 'System.Data.Objects.ObjectQuery'오류 유형의 개체를 캐스팅 할 수 없습니다. 이견있는 사람?

내가하려는 것은 지정된 조건의 행 목록을 가져온 다음 반환 된 모든 행을 삭제하는 것입니다.

답변

4

delete_old은 1 개 이상의 항목 일 수 있습니다.

ToList()을 추가하고 각 항목을 반복하고 각 열거 항목에 DeleteObject 메서드를 호출하십시오.

+0

그냥이 답변에 추가 할 수 있습니다 :'delete_old'는 하나의 레코드 만 발견하더라도 항상 IEnumerable입니다. – IronMan84

+0

감사! 이것은 크게 도움이되었습니다! – davemackey

관련 문제