1

I am using LINQ to ENTITIES to pull data from a SQL Server and I am trying to get just the first 255 characters from one of the columns who's data type is NTEXT. When I try I get the error:

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

The code producing the error is:

Subject = (yt.Message.Length > 255) ?  yt.Message.Substring(0, 255) :  yt.Message

Is there an easy way to either get the length so I can check it or just get the first 255 characters of an NTEXT column?

Trying to do yt.Message.ToString().Length gives the same error.

This is a third party DB for a forum software I am using and since I don't know anything about how that system works I want to stay away from altering any of the schema.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Matthew Verstraete
  • 5,958
  • 21
  • 63
  • 113

2 Answers2

0

It does not appear there is a way to do this in LINQ but what I ended up doing is creating a normal LINQ query to pull all the data then put a method in the ViewModel that returns the amount of characters I want.

public string Subject
    {
        get
        {
            return (yt.Message.Length > 255) ? yt.Message.Substring(0, 255) : yt.Message

        }
    }
Matthew Verstraete
  • 5,958
  • 21
  • 63
  • 113
-1

You need to use a view.

CREATE VIEW MyView
AS
    SELECT  MyKey,
            LEFT(CONVERT(NVARCHAR(MAX), MyValue), 255) AS MyValueSummary
    FROM    dbo.MyTable
Jonathan Allen
  • 66,142
  • 66
  • 246
  • 435