gpt4 book ai didi

c# - 通过将字符串类型的列表设置为字典来创建字典

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:34 25 4
gpt4 key购买 nike

我正在尝试使用字符串类型列表 (productIds) 创建此字典,但它出错了:

出错的部分是 p => p:无法将类型“字符串”隐式转换为“System.Collections.Generic.IEnumerable”

这对我来说没有意义,因为 p => p 使它成为可能,所以我将一个字符串传递到第一个参数,然后将一个新的产品类别列表传递到第二个参数。

Dictionary<string, IEnumerable<string>> missingProducts =
productIds.ToDictionary<string, IEnumerable<string>>(
p => p, p => p
new List<string>(productCategories));

这是我试图转换过来的 VB.NET 中的一个工作示例:

Dim productCategories As IList(Of String) = (From pc In prodCategories Select pc.CategoryName).ToList()

Dim missingProducts As Dictionary(Of String, IList(Of String)) = productIds.ToDictionary(Of String, IList(Of String))(Function(p) p, Function(p) New List(Of String)(productCategories))

最佳答案

ToDictionary的第二个参数也是一个Func(第一个:键选择器,第二个:值选择器),所以你必须传入p也在。

第二个:signature of the call to ToDictionary is wrong :

Dictionary<string, IEnumerable<string>> missingProducts =
productIds.ToDictionary<string, string, IEnumerable<string>>(
p => p,
p => new List<string>(productCategories));

关于c# - 通过将字符串类型的列表设置为字典来创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30765742/

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