LINQ的对.Substring抛出异常()抛出、异常、LINQ、Substring

2023-09-03 22:52:00 作者:做你怀中猫

我有一个情况我需要有我的LINQ to Entities查询返回依赖于字符串的长度的字符串。 这里的查询:

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:

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);

所以我在做什么越来越类型文件的最后10加入实体,然后选择一组属性,从它显示一个列表视图中。有些是普通的字符串(关注,发件人)。 CategoryCount返回它们与在文件对象相关联的类别的数目。

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.

不过,我想注释被截断如果它的长度,然后给定长度。在上述code,一切正常。现在,当我替换这行:

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:

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

这一行:

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

应用程序将引发XamlParseException(???)

the application throws a XamlParseException (???)

这符合指定绑定约束抛出异常的类型'DocumentManager.Views.ListEntriesView构造函数的调用

The invocation of the constructor on type 'DocumentManager.Views.ListEntriesView' that matches the specified binding constraints threw an exception

我真的不知道为什么它会做到这一点。不支持LINQ的子方法?

I really don't know why it would do that. Is the SubString method not supported in LINQ?

希望有人能帮助我在这里。在那之前,我就离开这事情是这样的。

Hope someone can help me here. Until then, I'll just leave it the way it is.

EDIT 2(不知怎么的,我的第一个编辑迷路了所以我重做吧): 根据我得到了我改变了我的code本的意见和它现在的工作:

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:

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提供商实现。

I forgot to mention that I'm using SQLite. So maybe Substring is not implemented in the SQLite EF Provider.

推荐答案

我不知道是肯定的,但我会怀疑子是不支持LINQ到实体。我会提出你拿(10),以你的SELECT语句前,再经过带(10)调用AsEnumerable(),然后让你以后的SELECT语句。这将导致你拉回来的文件集合从数据库中,然后将投影会在内存中完成的。

I don't know for sure, but I would suspect substring isn't supported by Linq-to-Entities. I would move your Take(10) to before your select statement, then after Take(10) call AsEnumerable(), then have your select statement after that. That will cause you to pull back a collection of Files from the database, then the projection will be done in-memory.