gpt4 book ai didi

c# - 加入等于一侧的比较键(startswith)

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:16 26 4
gpt4 key购买 nike

我加入了两个列表,一个包含总线路径,另一个包含作为总线子路径的磁盘路径。所以基本上我想加入 diskpath.startswith(buspath)。问题是 Join On 只允许在 equals 的一侧有一个键。

我想要这个:

Dictionary<string, string[]> disksPerBus;

Dictionary<string, string> busesHWPath; // key Bus Path, Value Bus name
Dictionary<string, string> disksHWPath; // Key Disk Path, Value Disk name

from busHWPath in busesHWPath
join diskHWPath in disksHWPath on diskHWPath.Key.StartsWith(busHWPath.Key)
...

最终目标是获取包含总线名称 (busHWPath.Value) 以及连接到该总线的所有磁盘 (diskHWPath.Value) 的字典 (disksPerBus)。

最佳答案

如果总线字典的值也是唯一的(您的问题表明):

Dictionary<string, string[]> disksPerBus = new Dictionary<string, string[]>();
foreach(var bkv in busesHWPath)
{
string[] disks = disksHWPath.Where(dkv => dkv.Key.StartsWith(bkv.Key))
.Select(dkv => dkv.Value).ToArray();
disksPerBus.Add(bkv.Value, disks); // this may fail if the value is not unique
}

关于c# - 加入等于一侧的比较键(startswith),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15762868/

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