0

In SQL Server table, one of the columne "question" has data type as "text".

Now, when i make query in sql then, use casting to varchar.

Now, in linq following is the query where retrieving specific result data.

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.

5
  • 3
    GROUP BY works like DISTINCT. I'm assuming one of your group expressions is a TEXT data type - Question would make the most sense. Is there a non-text field (question id?) that you can group on instead?
    – D Stanley
    Apr 16, 2015 at 16:23
  • yes, "question" is the field in group by clause as well as select clause. Apr 16, 2015 at 16:24
  • Then I would rearrange your query so that you're not grouping by a text field.
    – D Stanley
    Apr 16, 2015 at 16:26
  • can you please do that. i need same result which can be get through above query. Apr 16, 2015 at 16:28
  • I know exactly nothing about linq, do you you a select statement somewhere?
    – Andrew
    Apr 16, 2015 at 16:43

2 Answers 2

2

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)
 }
3
  • How to convert text to string in linq. Apr 16, 2015 at 17:02
  • How to convert text to string in linq. i mean, how can i apply substring to grab the data and make it string. if you can give example then, its helps. Apr 16, 2015 at 17:08
  • Category = t.Category.Substring(0) within the group clause is what does the trick, the rest of my changes were cleanup. Have you had a chance to try it?
    – jtimperley
    Apr 16, 2015 at 18:01
-2

The text datatype has been deprecated since 2005. You should not use it. It is a pain to work with. You should instead use varchar(max). I would recommend changing your table to varchar(max) instead.

3
  • ya,but now, it is not my hand. managed by client. i need to find some alternative here. this is existing database. Apr 16, 2015 at 16:32
  • Of course the database exists. It is a simple alter statement to use varchar(max) instead of text. I have done it plenty of times and there is no data loss at all.
    – Sean Lange
    Apr 16, 2015 at 16:36
  • ya, its true. but, not in my hand as it is in client control. for now, i need to find solution in linq or for loop where getting this exception. Apr 16, 2015 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.