[英]How can I make this query in Linq?
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.这意味着文件夹 docs 有 2 个子文件夹,文件夹 doku 没有子文件夹,backup 有 10 个子文件夹。
The SQL would go in that direction. SQL 将朝那个方向发展。 But I am also not sure if this is the best SQL query for it and I also need it in Linq但我也不确定这是否是最好的 SQL 查询,我在 Linq 中也需要它
Thanks in advance for your help在此先感谢您的帮助
Consider using DirectoryInfo.EnumerateDirectories考虑使用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所以现在我有一个 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.