【问题标题】:Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to 'System.Collections.Generic.List<string>'Linq - 无法将类型“System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>”隐式转换为“System.Collections.Generic.List<string>”
【发布时间】:2011-12-29 05:50:17
【问题描述】:

我收到以下错误:

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?)

我的代码如下:

 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>

【问题讨论】:

  • 能否提供Decks周围的节点结构

标签: c# linq


【解决方案1】:

我认为您应该检索一个列表 ,因为仅检索 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()

【讨论】:

  • 当我添加该行时,我的第一个“选择”短语出现错误:无法将类型 'System.Collections.Generic.IEnumerable' 隐式转换为 ' Munchkin.Model.PlayerProfiles.Profile'。存在显式转换(您是否缺少演员表?)我的 ; 上也出现语法错误- 说我少了一个逗号。我有: Decks = profiles.Elements("Decks").Descendants() .Select(x => x.Value).ToList(); }).FirstOrDefault();
  • 去掉ToList();中的分号
  • 半冒号修复了它。感谢您的建议!另外,我用正确的代码更新了我的原始帖子。对于我的节点结构,我需要 x.ToString() 来提取值。
【解决方案2】:

去掉“;” ToList() 之后

var profiles = xmlDoc.Element("PlayerPofiles").Elements("Player")
                     .where(profile =>(profile.Element("Active") == "True"))
                     .FirstOrDefault();

if(profiles!=null){
return 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 removed the ;
       Decks = profiles.Elements("Decks").Descendants() 
         .Select(x => x.Value.ToString()).ToList()
       }); 
}
else
{
//Handle if null
}

【讨论】:

    【解决方案3】:

    我建议你首先从 xml 中获取记录,然后进行如下映射:-

    var currentProfile = from profiles in xmlDoc.Element("PlayerPofiles").Elements("Player")
                         where (string)profiles.Element("Active") == "True".FirstOrDefault()
                         select profile
    Profile profile = new Profile();
    profile.Name = currentProfile.Name;
    . // all your properties mapping
    .
    .
    return profile;
    

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多