gpt4 book ai didi

c# - "The best overloaded Add method ' System.Collections.Generic.List.Add(System.Guid) ' for the collection initializer has some invalid arguments"

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

List<PageInfo> subPages = new List<PageInfo>();
// ...
// some code to populate subPages here...
// ...
List<Guid> subPageGuids = new List<Guid> {from x in subPages select x.Id}; //doesn't work

PageInfo 有一个 Guid 类型的 Id 字段。所以 x.Id 是一个 System.Guid。

上面的第二行代码不起作用...我收到错误:

The best overloaded Add method 'System.Collections.Generic.List.Add(System.Guid)' for the collection initializer has some invalid arguments

Argument '1': cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Guid'

我只用 C# 编写了大约一周的代码,但我以前做过类似的模式,但从未遇到过这个问题。

最佳答案

我想你想要:

List<Guid> subPageGuids = new List<Guid>(from x in subPages select x.Id);

(注意大括号变成了普通大括号)

这将调用以 IEnumerable(这是 linq 查询返回的内容)作为参数的 List 的构造函数。现在您正在尝试使用 object initializer 的语法.

关于c# - "The best overloaded Add method ' System.Collections.Generic.List.Add(System.Guid) ' for the collection initializer has some invalid arguments",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/498482/

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