使用DataContext时,有效的Linq方法失败?

时间:2018-10-17 01:52:08

标签: c# sql linq datacontext

我正在尝试使用LinqToSQL和DataContext将一组有效的Linq查询应用于数据库。下面是一种查询文件名列表并返回简单数据的有效方法。

附加的方法可以编译。该方法与随附的List完美配合。但是,当我在DataContext上尝试它时,它失败,并显示“类型'System.String'不支持序列运算符”错误。它不会在编译时失败,而是在运行时失败。

DataContext中的数据与包含的列表完全相同,尽管还有一些其他列。请帮助我了解为什么当查询在列表上工作时DataContext不批准查询。

    private static void LinqDiscoverySnippets()
    {
        List<string> lines = new List<string>
        {
            @"D:\tree.txt",
            @"D:\Deciduous\tree.doc",
            @"D:\Deciduous\topfiles.txt",
            @"D:\D\e\c\i\d\u\o\u\s\tree.txt",
            @"D:\Deciduous\BroadleafTree.docx",
            @"D:\Deciduous\BroadleafTree123.pdf",
            @"D:\Deciduous\BroadleafTree141182.pdf",
            @"D:\Deciduous\readMe_folder_Organization_here_to_save_some_trees.txt"
        };
        // All the following lines work as expected
        string shortest = lines.OrderBy(s => s.Length).First();
        string longest = lines.OrderByDescending(s => s.Length).First();
        string mostest = lines.OrderByDescending(v => v.Count(c => c == Path.DirectorySeparatorChar)).First();
        string leastest = lines.OrderBy(v => v.Count(c => c == Path.DirectorySeparatorChar)).First();
        var mostestCount = mostest.Count(f => f == Path.DirectorySeparatorChar);
        var leastestCount = leastest.Count(f => f == Path.DirectorySeparatorChar);

        // I have a database with the exact same information, it has some additional columns
        TransformDataContext transform = new TransformDataContext();

        // these two appear to be working as expected, I had to add the column name which is "FULLNAME"
        string shortestString = transform.Documents.OrderBy(s => s.FULLNAME.Length).First().FULLNAME;
        string longestString = transform.Documents.OrderByDescending(s => s.FULLNAME.Length).First().FULLNAME;

        // tries to modify these the exact same way and get "Sequence operators not supported for type 'System.String'"
        var mostFolders = transform.Documents.OrderByDescending(v => v.FULLNAME.Count(c => c == Path.DirectorySeparatorChar)).First();
        var leastFolders = transform.Documents.OrderBy(v => v.FULLNAME.Count(c => c == Path.DirectorySeparatorChar)).First();

        // cant get the above to work so have not tried these yet
        var mostFolderCount = mostFolders.Count(f => f == Path.DirectorySeparatorChar);
        var leastFolderCount = leastFolders.Count(f => f == Path.DirectorySeparatorChar);
    }

0 个答案:

没有答案