代码之家  ›  专栏  ›  技术社区  ›  krishna

Linq SQL错误:无法将文本数据类型选择为distinct,因为它不可比较

  •  1
  • krishna  · 技术社区  · 17 年前

    我想从问题表中选择所有尚未包含在特定测验中的问题。 我的问题是,为什么下面的代码会因消息而失败:

    无法将文本数据类型选择为distinct,因为它不可比较。 数据类型文本和文本在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);
                    }
                }
            }
    
    1 回复  |  直到 17 年前
        1
  •  4
  •   Jon Skeet    17 年前

    这实际上是一个SQL问题,而不是一个LINQ问题,而不是 Except Linq Call显然最终被翻译为 DISTINCT SQL中的子句。

    您可以让linq to对象为您执行“except”部分:

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

    呼唤 AsEnumerable 只需强制它使用linq to对象执行后续的进程内操作,而不是将其全部转换为SQL查询。

    显然,这是非常低效的,但它比目前可能出现的O(n^3)循环要好:)

    search for the error message (第一部分)给出了一些在SQL方面可能有用的结果。我建议您将正在生成的SQL记录下来,然后检查它并读取这些命中值——它们可能会建议您修改模式,以便在数据库中完成所有操作。