2011-12-29 104 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

相關問題