2011-12-29 10 views
4

私は、次のエラーを取得しています:のLINQ - 暗黙のうちに 'System.Collections.Generic.List <string>' にタイプ 'System.Collections.Generic.IEnumerable <System.Xml.Linq.XElement>' に変換できません

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

した後、あなたはデッキ –

答えて

7

リスト<文字列を取得する必要があると思います>、Descendentsを取得するだけで[XElementのIEnumerable]が表示されるためです。 (http://msdn.microsoft.com/en-us/library/bb337483.aspx)しかし、あなたが必要とするのは、タイプstringのIEnumerableです。あなたがリストに埋めるために必要なものを「requiredname」を交換してください:私はあなたが最初のXMLからレコードをフェッチし、次のようなマッピングを行うことであることをお勧め

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

周りのノード構造を提供することができます。 IEnumerable 'を' Munchkin.Model.PlayerProfiles.Profile 'に設定します。明示的な変換が存在します(キャストがありませんか?) 私の構文エラーも発生しています。 - 私はカンマが足りないと言っています。私は: デッキ= profiles.Elements( "デッキ")。子孫() .Select(x => x.Value).ToList(); })。FirstOrDefault(); – Yecats

+0

ToList();のセミコロンを削除します – V4Vendetta

+0

セミコロンで固定しました。提案していただきありがとうございます!また、元の投稿を正しいコードで更新しました。私のノード構造では、値を取得するためにx.ToString()が必要でした。 – Yecats

0

何: -

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

";" 暗黙的に型「System.Collections.Genericを変換できません:私は私の最初のフレーズは、「選択」のエラーが出る中でその行を追加すると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

関連する問題