Linq throws an exception in .Substring ()

I have a situation where I need the LINQ to Entities query to return a substring depending on the length of the string. Here Request:

var query = (
    from f in Context.Files
    orderby f.DateAdded descending
    select new
    {
        Concerns = f.Concerns.Name,
        Sender = f.Sender.Name,
        CategoryCount = f.Categories.Count(),
        DateAdded = f.DateAdded,
        Comment = (f.Comment == null || f.Comment.Length < 5) 
            ? f.Comment : f.Comment
    }).Take(10);

So what I do, I get the last 10 added objects of type "Files", and then select a set of properties from it to display inside the list. Some of them are simple strings (Concerns, Sender). CategoryCount returns the number of categories associated with the File object.

However, I want the comment to be truncated if it is longer than the given length. In the above code, everything works correctly. Now when I replace this line:

Comment = (f.Comment == null || f.Comment.Length < 5) 
    ? f.Comment : f.Comment

Using this line:

Comment = (f.Comment == null || f.Comment.Length < 5) 
    ? f.Comment : f.Comment.SubString(0,5)

application throws XamlParseException (???)

"DocumentManager.Views.ListEntriesView", ,

, . SubString LINQ?

, - . , .

EDIT 2 (- , ): , , :

var query = App.Context.Files.OrderByDescending(File => File.DateAdded).Take(10).AsEnumerable()
            .Select(File => new
            {
                Concerns = File.Concerns.Name,
                Sender = File.Sender.Name,
                CategoryCount = File.Categories.Count(),
                DateAdded = File.DateAdded,
                Comment = (File.Comment == null || File.Comment.Length < 100) ? File.Comment : File.Comment.Substring(0, 100) + "..."
            });

, SQLite. , , SQLite EF.

+2
4

, , Linq-to-Entities. Take (10) select, Take (10) AsEnumerable(), select. , .

+2

LINQ. IQueryable, , ( ). AsEnumerable - , .

, IQueryable IEnumerable :

http://msdn.microsoft.com/en-us/vcsharp/ff963710

+3

, , SQLite,

  • SQL Server LINQ to Entities

  • SQL-, SQL

  • SQLite substr,

, .

.

EntityContainer

<Function Name="substr" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" ReturnType="nvarchar">
  <Parameter Name="text" Type="nvarchar" Mode="In" />
  <Parameter Name="startPos" Type="int" Mode="In" />
</Function>

class ( )

[DbFunction("MyModel.Store", "substr")]
public string SubStr(string text, int startPos) {
    return text.Substring(startPos);
}

context.SubStr(text, startpos)

SUBSTR SUBSTRING! , , .

, !

+1

That's right - LINQ does not support substring, but it is not always very clear from the exception when you try so unsuccessfully to do such things.

0
source

All Articles