Search code examples
c#entity-frameworklinqntext

"Argument data type ntext is invalid for argument 1 of len function" error


I'm using entity framework and I write this code to get some result from DB:

ReviewsDBEntities DB = new ReviewsDBEntities();  
var result=DB.Review.Where(r => r.ReviewText.Length > 200);

But I get this error as an inner error: "Argument data type ntext is invalid for argument 1 of len function"

I looked it up, and I found out that because the type of ReviewText is defined as ntext, the function Len won't run it on the Database side. Now, I don't know how I can change the code to get ReviewTexts with Length more than 200.


Solution

  • var result=DB.Review.Where(r => SqlFunctions.DataLength(r.ReviewText) / 2 > 200);
    

    Why / 2? Because DATALENGTH returns the length in bytes, and NTEXT contains Unicode characters, each of which consume 2 bytes.