gpt4 book ai didi

c# - 根据不等于的条件加入两个列表

转载 作者:行者123 更新时间:2023-11-30 22:17:08 26 4
gpt4 key购买 nike

简而言之...

我有一个源代码管理文件夹列表,这些文件夹是使用 TFS API 从变更集中收集的,结构如下:

$/ProjectFolder/BranchName/Project A

$/ProjectFolder/BranchName/Subfolder/Project B

$/ProjectFolder/BranchName/Project C

上面只会包含一些选定的项目,然后我会以以下格式列出所有项目名称:

Project A

Project B

Project C

Project D

我试图选择一个在两个列表之间有连接的新列表,然后得到一个包含第一个结构和第二个结构的匿名类型。我想我应该使用 Contains 之类的东西,但是当尝试使用连接时,语法只提供 equals

我试过:

var sourceFolders = new List<string>()
{
"$/ProjectFolder/BranchName/Project A",
"$/ProjectFolder/BranchName/Sub-Folder/Project B",
"$/ProjectFolder/BranchName/Project C"
};
var projectNames = new List<string>() { "Project A", "Project B", "Project C", "Project D"};
var filteredProjects = sourceFolders.Where(sourceFolder => projectNames.Any(x => sourceFolder.Contains(x)));

filteredProjects 将只包含 sourceFolders 而不是它也映射了哪个 project...

有什么想法吗?

期望的输出

new { SourceFolder = "$/ProjectFolder/BranchName/Project A", ProjectName  = "Project A" };
new { SourceFolder = "$/ProjectFolder/BranchName/Subfolder/Project B", ProjectName = "Project B" };
new { SourceFolder = "$/ProjectFolder/BranchName/Project C", ProjectName = "Project C" };

最佳答案

或者你可以这样做:

var filteredProjects = from source in sourceFolders
let project = projectNames.FirstOrDefault(p => source.Contains(p))
where project != null
select new {
SourceFolder = source,
Project = project
};

关于c# - 根据不等于的条件加入两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16915580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com