2011-12-29 102 views
4

我收到以下错误:LINQ的 - 无法隐式转换类型 'System.Collections.Generic.IEnumerable <System.Xml.Linq.XElement>' 到 'System.Collections.Generic.List <string>'

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>[email protected]</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> 

+0

可以提供周围甲板 – 2011-12-29 06:21:51

回答

7

我想你应该检索一个名单< string>,因为只是检索后代会给你一个[IEnumerable为XElement]。 (http://msdn.microsoft.com/en-us/library/bb337483.aspx)但是,您需要的是一个类型为string的IEnumerable。

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

节点结构。当我再补充一点线,我得到我的第一次“选择”这句错误: 无法隐式转换类型“System.Collections.Generic。 IEnumerable '到'Munchkin.Model.PlayerProfiles.Profile'。存在明确的转换(您是否缺少演员?) 我也收到我的语法错误; - 说我错过了一个逗号。我有: Decks = profiles.Elements(“Decks”)。Descendants() .Select(x => x.Value).ToList(); })。FirstOrDefault(); – Yecats 2011-12-29 06:05:34

+0

在'ToList();中删除分号;' – V4Vendetta 2011-12-29 06:19:36

+0

固定它的semii冒号。感谢您的建议!此外,我用正确的代码更新了我的原始帖子。对于我的节点结构,我需要x.ToString()来提取值。 – Yecats 2011-12-29 17:24:42

0

我建议你是先取从XML的记录,然后做映射类似以下:你需要在列表,以填补什么替换“requiredname” -

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; 
1

删除“;” 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 
} 
+1

啊,修好了......谢谢! – Yecats 2011-12-29 17:24:18

相关问题