无法将文本数据类型选择为DISTINCT,因为它不可比较.没有做明确的操作 [英] The text data type cannot be selected as DISTINCT because it is not comparable. Not doing distinct operation
问题描述
在SQL Server表中,列问题"之一的数据类型为文本".
In SQL Server table, one of the columne "question" has data type as "text".
现在,当我在sql中进行查询时,请使用强制转换为varchar.
Now, when i make query in sql then, use casting to varchar.
现在,在linq中,以下是检索特定结果数据的查询.
Now, in linq following is the query where retrieving specific result data.
问题是,当我尝试迭代FOR
或FOREACH
循环中的结果集时,它给了我以下错误.我用谷歌搜索,但它说是在做独特操作时发生的,但我没有进行独特操作.
The problem is, it gives me below error when i try to iterate result set in FOR
OR FOREACH
loop. I googled but, it says , that happen when do distinct but, i am not doing distinct operation.
The text data type cannot be selected as DISTINCT because it is not comparable
from t in db.Table
group t by new
{
t.Category,
t.Question
} into g
order by g.Category
select new
{
CategoryName = t.FirstOrDefault().Category, //might be required to handle null here
Question = t.FirstOrDefault().Question, //might be required to handle null here
TotalCount = t.Count(),
AnsLessEqual3 = t.Where(d => d.Answer<=3).Count(),
Ans5 = t.Where(d => d.Answer = 5).Count(),
Ans789 = t.Where(d => d.Answer = 7 || d.Answer = 8 || d.Answer = 9).Count()
}
请指导如何解决此问题.
Please guide how to fix this.
推荐答案
您必须获得LINQ才能将文本字段转换为varchar,抓住整个Substring应该可以解决问题.另外,您应该属于自己的小组,否则,假设LINQ允许,您将汇总整个表.
You'll have to get LINQ to convert your text field to a varchar, grabbing the entire Substring should do the trick. Also, you should stick within your group, otherwise you will be summarizing the entire table, assuming LINQ would allow it.
from t in db.Table
group t by new
{
Category = t.Category.Substring(0),
t.Question
} into g
orderby g.Key.Category
select new
{
CategoryName = g.Key.Category,
Question = g.Key.Question,
TotalCount = g.Count(),
AnsLessEqual3 = g.Count(d => d.Answer <= 3),
Ans5 = g.Count(d => d.Answer = 5),
Ans789 = g.Count(d => d.Answer = 7 || d.Answer = 8 || d.Answer = 9)
}
这篇关于无法将文本数据类型选择为DISTINCT,因为它不可比较.没有做明确的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!