2009-08-20 7 views
1

특정 퀴즈에 포함되지 않은 질문 표에서 모든 질문을 선택하고 싶습니다. 제 질문은 아래의 코드가 메시지와 함께 실패하는 이유입니다.linq sql 오류 : 텍스트 데이터 형식을 비교할 수 없기 때문에 별개로 선택할 수 없습니다.

텍스트 데이터 유형은 비교할 수 없으므로 DISTINCT로 선택할 수 없습니다. 데이터 유형 text 및 text는 is 연산자에서 호환되지 않습니다. 이 잘 작동으로

var allQuestions = from q in my.Questions 
         select new 
         { 
          Select = new Boolean(), 
          Id = q.QuestionId, 
          QuestionName = q.Name, 
          QuestionText = q.Text, 
          Topic = q.Topic.Title 
         }; 

     var currentQuestions = from cq in my.QuizQuestions 
          where cq.Quiz.quizId == quizId 
          select new 
          { 
           Select = new Boolean(), 
           Id = cq.Questions.QuestionId, 
           QuestionName = cq.Questions.Name, 
           QuestionText = cq.Questions.Text, 
           Topic = cq.Questions.Topic.Title 
          }; 
var selectQuestions = allQuestions.Except(currentQuestions); 

는 :

var allQuestions = (from q in my.Questions 
         select new 
         { 
          Select = new Boolean(), 
          Id = q.QuestionId, 
          QuestionName = q.Name, 
          QuestionText = q.Text, 
          Topic = q.Topic.Title 
         }).ToList(); 

     var currentQuestions = (from cq in my.QuizQuestions 
          where cq.Quiz.quizId == quizId 
          select new 
          { 
           Select = new Boolean(), 
           Id = cq.Questions.QuestionId, 
           QuestionName = cq.Questions.Name, 
           QuestionText = cq.Questions.Text, 
           Topic = cq.Questions.Topic.Title 
          }).ToList(); 


     int allquestionsCount = allQuestions.Count(); 
     for (int i = allquestionsCount; i < 0; i--) 
     { 
      foreach(var question in currentQuestions){ 
       if (question.Id.Equals(allQuestions.ElementAt(i - 1).Id)) 
       { 
        allQuestions.RemoveAt(i - 1); 
       } 
      } 
     } 

답변

4

이 정말 DISTINCT 절로 번역 Except LINQ 호출이 분명히 끝 사실 이외의 SQL 질문보다는 LINQ 질문입니다 SQL에서.

당신은 당신을 위해 일부 "제외"를 할 객체에 LINQ를 얻을 수 : 그냥 변환하는 대신 객체에 LINQ와 함께 후속 작업의 프로세스를 수행하도록 강제

var allQuestions = // code as before in first example 
var currentQuestions = // code as before in first example 
var selectQuestions = allQuestions.AsEnumerable() 
            .Except(currentQuestions); 

호출 AsEnumerable에 모든 SQL 쿼리에.

은 분명히 그것은 매우 비효율적이다, 그러나 잠재적으로 O (N^3) 루프는 순간 :)

search for the error message (첫 번째 부분)는 SQL의 측면에서 잠재적으로 유용한 결과를 제공에있어보다 낫다 그래도. 난 당신이 생성되는 SQL을 로그, 다음 그것을 검사하고 그 안타를 읽는 것이 좋습니다 - 그들은 모두 데이터베이스에서 할 수 있도록 스키마를 변경하는 방법을 제안 수 있습니다.

+0

감사합니다. Jon, 그 작품. 다른 테이블에 대해 잘 작동하는 비슷한 쿼리가 있습니다. 나는이 경우 문제가 questiontext 필드의 데이터 유형과 관련이 있다고 생각한다. 이것은 'text'유형이다. – krishna

+0

나중에 내 가정을 테스트 할 것입니다. 다시 감사드립니다. – krishna

+1

실제로, 생성 된 SQL을 살펴볼 가치가 있습니다. 나는 당신의 다른 질문이 왜 효과가 있는지 설명 할 것입니다. 솔직히 말하면, 텍스트 컬럼에서 작동하도록 스키마를 변경할 수있는 방법이있을 수 있습니다. –

관련 문제