5

This is my error message The data types text and varchar are incompatible in the equal to operator. My code is something like

var result = db.Table.Where(x => x.columnA == "A").ToList();

I know the reason is the columnA's type in DB is text so they can't match and I can't update the DB scheme.

Could I do this by using LINQ to SQL?

Update

This is my DAO

[Required]
[StringLength(10)]
public string MessageName { get; set; }
1
  • 1
    does that work? var result = db.Table.Where(x => x.columnA.ToString() == "A").ToList(); Commented Jun 13, 2016 at 9:08

5 Answers 5

9

TEXT columns do not support the equal to operator, even in SQL.

You must make it so your query translates into a LIKE operator, which can be achieved with either Contains (LIKE '%str%'), StartsWith (LIKE '%str') or EndsWith (LIKE '%str').

If you need strict equality, something like this should work:

var result = db.Table.Where(x => x.columnA.StartsWith("A") &&
                                 x.columnA.EndsWith("A")).ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

I think it will wok and thanks for your explain but it take too loooong time, haha.
While you are right in general, that sample doesn't provide a strict equality. For example a field containing "A blah blah A" would match. You can provide == with Linq To SQL using ToString(). However, it would be nonsense to do == on a Text field.
@CetinBasoz you are right. It's a old project and I can't update that part
didnt work, i got Argument data type ntext is invalid for argument 1 of left function
4

you can use this (i haven't tested the code)

var result = db.Table.Where(x => x.columnA.Contains("A")).ToList();

Comments

1

Can you cast the Column in question to a string? I haven't got any way to test this as I don't have a DB with a Text type.

var result = db.Table.Where(x => x.columnA.ToString() == "A").ToList();

6 Comments

It is string at begining
This one is the actual answer to the OP's question.
@CetinBasoz This will cause same error at my question. And you can see the Update part in my question. columnA is string at begining.
No it wouldn't cause an error with Linq To SQL and just work. What do you mean, "columnA is string at beginning"? What else could it be, if it is text or varchar(), ...?
I'm not convinced you can use an equality compare with text, it's a data-type that I have never used, but I do recall reading about it's issues with LINQ. If you look at the model that's been generated, you will see what ColumnA looks like.
|
1

I tried all the above-mentioned solution and none of the fixes works for me, So here I am posting the fix which works for me

 var result = db.Table.Where(x => Convert.ToString(x.columnA) == "A").ToList();

2 Comments

This is better than @christiandev's answer because it will not throw an exception when x.columnA is null or empty.
This gives me this error: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
0

I had the same issue and none of the string conversion solutions worked for me. The problem was that I had a 'text' data type for this column (because I migrated it from sqlite). Solution: just change the data type to `'nvarchar()' and regenerate the table.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.