I have a list of paths in a database.
\\apollon\HardDev\01_Elektronik\***
\\apollon\Sales\Kunden\S\***
\\apollon\HardDev\02_Optik\Optik\***_Projekte_Salb\***
\\apollon\Sales\Kunden\O\***\Auftrag 2002_2008-09-09
\\apollon\Sales\Kunden\H\***\Auftrag 4534_2013-07-26
\\apollon\User\***\quickies_2016\BSI_screenshots\Neuer Ordner
\\apollon\Sales\Kunden\G\***\Auftrag 2153_2009-06-24
\\apollon\HardDev\01_Elektronik\***\bestuecker_afem_v12
\\apollon\User\***\quickies_2015\src_***
\\apollon\Sales\Kunden\H\***\4352IO_2013-06-07
etc.
Now I needed a query that gets a path as a parameter and only returns to me the direct subfolder and whether there are subfolders in which.
EXAMPLE:
Function gets the path
\\apollon\User\Walzenbach
and as result I would like to know that the path has the following subfolders
{dir = "\\apollon\User\Walzenbach\docs", subfolderCount = 2}
{dir = "\\apollon\User\Walzenbach\doku", subfolderCount = 0}
{dir = "\\apollon\User\Walzenbach\backup", subfolderCount = 10}
That means the folder docs has 2 subfolders, the folder doku has no subfolders and backup has 10 subfolders.
The SQL would go in that direction. But I am also not sure if this is the best SQL query for it and I also need it in Linq
Thanks in advance for your help
Consider using DirectoryInfo.EnumerateDirectories
string directoryName = "\\apollon\User\Walzenbach"
DirectoryInfo directory = new DirectoryInfo(directoryName);
// TODO: exception if !directory.Exists;
var result = directory.EnumerateDirectories()
.Select(subDirectory => new
{
SubDirectory = subDirectory, // This is a DirectoryInfo
// if you prefer the name, instead of the Directory:
SubDirectoryName = subDirectory.Name,
// count the number of subfolders of this subfolder:
SubFolderCount = subFolder.EnumerateDirectories().Count(),
});
Simple comme bonjour!
Here's a suggestion :
var query=from d in directories
join sf in directories on d.Path equals sf.ParentPath into grp1
from sf in grp1.DefaultIfEmpty()
let result=new {dir=d,sub=sf}
where reault.dir.Path.StartsWith(path)
group result by result.dir.Path into grp2
select new {dir=grp2.Key,subFolderCount=grp2.Count()};
So now I have a SQL that at least reasonably does what I want ... But Linqer can not translate it into Linq
SELECT [Directory], subfolderCount =
(SELECT COUNT([Directory])
FROM [ArgesPerm].[argarm].[dirs]
WHERE lower([Directory]) LIKE '\\apollon\user\walzenbach\%'
GROUP BY [Directory])
FROM [ArgesPerm].[argarm].[dirs]
WHERE lower([Directory]) LIKE '\\apollon\user\walzenbach\%'
AND LEN(SUBSTRING([Directory], 3, 10000)) - Len(Replace(SUBSTRING([Directory], 3, 10000), '\', '')) < 3
GROUP BY [Directory]
ORDER BY [Directory]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.