[英]How can I make this query in Linq?
我有一個數據庫中的路徑列表。
\\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.
現在我需要一個查詢來獲取路徑作為參數,並且只返回直接子文件夾以及其中是否有子文件夾。
例子:
函數獲取路徑
\\apollon\User\Walzenbach
結果我想知道該路徑具有以下子文件夾
{dir = "\\apollon\User\Walzenbach\docs", subfolderCount = 2}
{dir = "\\apollon\User\Walzenbach\doku", subfolderCount = 0}
{dir = "\\apollon\User\Walzenbach\backup", subfolderCount = 10}
這意味着文件夾 docs 有 2 個子文件夾,文件夾 doku 沒有子文件夾,backup 有 10 個子文件夾。
SQL 將朝那個方向發展。 但我也不確定這是否是最好的 SQL 查詢,我在 Linq 中也需要它
在此先感謝您的幫助
考慮使用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(),
});
簡單的來吧!
這是一個建議:
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()};
所以現在我有一個 SQL 至少可以合理地做我想要的......但是 Linqer 無法將它翻譯成 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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.