How to rewrite this LINQ expression using consistent syntax?

This request with one instruction says neatly, "Give me a list of the bare file name for which the file is a ZIP repository containing a specific file structure."

But I use both the .Where () extension method (free syntax) and the select request, because everything I try to execute cannot be compiled. If I change ".Where (file ==> <statement>)" to "where <statement>", I get an error that the anonymous method code does not return bool, and if I change "select <item>" to " . Select (<clause>) ", error:" No select clause is used. "

I am satisfied with either the request or the free syntax, but I would like to dwell on this or that. Can someone explain why this does not work and what I need to do to set up one consistent syntax?

return (from file in Directory.EnumerateFiles(
                    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Globals.CompanyName, ProjectName, FolderName),
                    imageExtension,
                    SearchOption.TopDirectoryOnly)
    .Where(file =>
    {
        try
        {
            string relativePath = ClassFru.Station + "/";   // Inside ZIPs, paths use a single forward slash
            var zip = new ZipFile();
            zip.ZipError += (s, o) => { throw new Exception(); };
            using (zip = ZipFile.Read(file))
            {
                /// <todo>if (zip.Comment != Globals.CompanyName) { return false; }</todo>
                foreach (var fru in this.gFrus)
                {
                    var fruPath = relativePath + fru.Id + '.';
                    if (!(from e in zip where !e.IsDirectory && e.FileName.StartsWith(fruPath) select true).Any()) { return false; }
                }
                return true;
            }
        }
        catch (Exception)
        {
            return false;
        }
    })
    select Path.GetFileNameWithoutExtension(file)).ToArray();
+4
source share
3 answers

Since I don’t have the whole type that you use in this expression, it is quite difficult to compile it, but I think I should work like this:

            return (Directory.EnumerateFiles(
            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                Globals.CompanyName, ProjectName, FolderName),
            imageExtension,
            SearchOption.TopDirectoryOnly)
            .Where(file => {
                try
                {
                    string relativePath = ClassFru.Station + "/"; // Inside ZIPs, paths use a single forward slash
                    var zip = new ZipFile();
                    zip.ZipError += (s, o) => {
                        throw new Exception();
                    };
                    using (zip = ZipFile.Read(file))
                    {
                        /// <todo>if (zip.Comment != Globals.CompanyName) { return false; }</todo>
                        foreach (var fru in this.gFrus)
                        {
                            var fruPath = relativePath + fru.Id + '.';
                            if(zip.Any(e=> !e.IsDirectory && e.FileName.StartsWith(fruPath))
                                    .Any())
                            {
                                return false;
                            }
                        }
                        return true;
                    }
                } catch (Exception)
                {
                    return false;
                }
            }).Select(Path.GetFileNameWithoutExtension).ToArray());
+3
source

select Select(file => from file in . select. from, , . from [...] in select.

+1

, , . - , , , ?

, LINQ . LINQ, , , ( ), .

If you simply delete part of fromyour request, you can easily get the method syntax to work as ISun shows you how to do this .

On the other hand, your code will be much more understandable and module if you just extract your anonymous method like @Servy suggested in the comments. In this case, you can either say where FileHasMatchingZipStructure(file). or .Where(FileHasMatchingZipStructure)as you like.

0
source

All Articles