【问题标题】:linq sql error : the text data type cannot be selected as distinct because it is not comparablelinq sql 错误:文本数据类型不能选择为不同的,因为它不可比较
【发布时间】:2009-08-20 13:31:58
【问题描述】:

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

文本数据类型不能选择为 DISTINCT,因为它不可比较。 is 运算符中的数据类型 text 和 text 不兼容。

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);
                }
            }
        }

【问题讨论】:

    标签: linq


    【解决方案1】:

    这实际上是一个 SQL 问题,而不是一个 LINQ 问题,除了 Except LINQ 调用显然最终翻译为 SQL 中的 DISTINCT 子句这一事实。

    您可以让 LINQ to Objects 为您完成“除外”部分:

    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 Objects 在进程中执行后续操作,而不是将其全部转换为 SQL 查询。

    显然这是非常低效的,但它比你目前拥有的潜在 O(n^3) 循环要好:)

    search for the error message(第一部分)在 SQL 方面提供了一些可能有用的结果。我建议您记录正在生成的 SQL,然后对其进行检查并阅读这些命中 - 他们可能会建议更改架构以允许在数据库中完成所有操作的方法。

    【讨论】:

    • 感谢乔恩,这行得通。我有一个类似的查询,它适用于另一个表。我认为在这种情况下,问题在于 questiontext 字段的数据类型。它是“文本”类型。
    • 稍后会测试我的假设。再次感谢。
    • 确实——这就是为什么值得查看生成的 SQL 的原因。我怀疑这将解释为什么您的其他查询有效。可能有一些方法可以更改架构以使其适用于文本列 - 老实说,我不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多