Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq throws exception on .Substring()
    text
    copied!<p>I've got a situation where I need to have my LINQ to Entities query return a substring depending on the length of the string. Here's the Query:</p> <pre><code>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 &lt; 5) ? f.Comment : f.Comment }).Take(10); </code></pre> <p>So what I'm doing is getting the last 10 added Entities of type Files and then select a set of properties from it to display inside a listview. Some are plain strings (Concerns, Sender). CategoryCount returns the number of categories which are associated with the File object.</p> <p>However, I want the comment to be truncated if it is longer then a given length. In the above code, everything is working correctly. Now when I replace this line:</p> <pre><code>Comment = (f.Comment == null || f.Comment.Length &lt; 5) ? f.Comment : f.Comment </code></pre> <p>With this line:</p> <pre><code>Comment = (f.Comment == null || f.Comment.Length &lt; 5) ? f.Comment : f.Comment.SubString(0,5) </code></pre> <p>the application throws a XamlParseException (???)</p> <blockquote> <p>The invocation of the constructor on type 'DocumentManager.Views.ListEntriesView' that matches the specified binding constraints threw an exception</p> </blockquote> <p>I really don't know why it would do that. Is the SubString method not supported in LINQ?</p> <p>Hope someone can help me here. Until then, I'll just leave it the way it is.</p> <p>EDIT 2 (Somehow, my first edit got lost. So I'm redoing it): Based on the comments I got I changed my code to this and it works now:</p> <pre><code>var query = App.Context.Files.OrderByDescending(File =&gt; File.DateAdded).Take(10).AsEnumerable() .Select(File =&gt; new { Concerns = File.Concerns.Name, Sender = File.Sender.Name, CategoryCount = File.Categories.Count(), DateAdded = File.DateAdded, Comment = (File.Comment == null || File.Comment.Length &lt; 100) ? File.Comment : File.Comment.Substring(0, 100) + "..." }); </code></pre> <p>I forgot to mention that I'm using SQLite. So maybe Substring is not implemented in the SQLite EF Provider.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload