gpt4 book ai didi

c# - Linq - 无法将类型 'System.Collections.Generic.IEnumerable' 隐式转换为 'System.Collections.Generic.List'

转载 作者:太空狗 更新时间:2023-10-30 00:26:59 25 4
gpt4 key购买 nike

我收到以下错误:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

我的代码如下:

 public Profile PullActiveProfile()
{
//currentProfile.Decks = new List<string>();
return currentProfile = (from profiles in xmlDoc.Element("PlayerPofiles").Elements("Player")where (string)profiles.Element("Active") == "True"
select new Profile
{
Name = (string)profiles.Element("Name"),
Type = (string)profiles.Element("Type"),
Verified = (string)profiles.Element("Verified"),
Password = (string)profiles.Element("Password"),
Email = (string)profiles.Element("Email"),
Sex = (string)profiles.Element("Sex"),
Avatar = (string)profiles.Element("Avatar").Attribute("path") ?? "",
Created = (DateTime)profiles.Element("Created"),
Birthday = (string)profiles.Element("Birthday") ?? "",
Wins = (string)profiles.Element("Ratio").Element("Win") ?? "0",
Losses = (string)profiles.Element("Ratio").Element("Loss") ?? "0",
Abandoned = (string)profiles.Element("Ratio").Element("Abandoned"),
// The following line is where I get the error. The decks node can have many descendants
Decks = profiles.Elements("Decks").Descendants()
.Select(x => x.ToString()).ToList();
}).FirstOrDefault();
}

这是节点结构:

<PlayerPofiles>
<Player>
<Name>Stacey - Online</Name>
<Type>Full/Basic</Type>
<Active>True</Active>
<Verified>True</Verified>
<Password>pass</Password>
<Email>xxxx@gmail.com</Email>
<Sex>Female</Sex>
<Avatar path="/images/Treasure/BroadSword.png" />
<Ratio>
<Win>0</Win>
<Loss>0</Loss>
<Abandoned>0</Abandoned>
</Ratio>
<Created>6/19/2011</Created>
<Birthday>09/28/1989</Birthday>
<Decks>
<Base />
<Booty />
</Decks>
</Player>

最佳答案

我认为您应该检索一个列表 ,因为只要检索 Descendents 就会给您一个 [IEnumerable for XElement]。 ( http://msdn.microsoft.com/en-us/library/bb337483.aspx ) 但您需要的是 string 类型的 IEnumerable。将'requiredname'替换为您需要在列表中填写的内容:

profiles.Elements("Decks").Descendants()
.Select(x => x.requiredname).ToList()

关于c# - Linq - 无法将类型 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' 隐式转换为 'System.Collections.Generic.List<string>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8664450/

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