2011-12-25 12 views
1
私は次のエラー受け付けております

変換できません:あなたのcurrentProfileがシングルとしてのLINQ - 暗黙的に型「System.Collections.Generic.IEnumerable

Profile currentProfile; 

public Profile ActiveProfile() 
{ 
    currentProfile = new Profile();   
    return currentProfile = 
     (from profiles in xmlDoc.Element("PlayerPofiles").Element("Online").Elements("Player") 
     where (string)profiles.Element("Active") == "True" 
     select new Profile 
     { 
      Name = (string)profiles.Element("Name"), 
      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"), 
      Losses = (string)profiles.Element("Ratio").Element("Loss"), 
      Abandoned = (string)profiles.Element("Ratio").Element("Abandoned") 
     }); 
} 

答えて

5
public Profile ActiveProfile() 
     { 
      currentProfile = new Profile(); 

      return currentProfile = (from profiles in xmlDoc.Element("PlayerPofiles").Element("Online").Elements("Player") 
          where (string)profiles.Element("Active") == "True" 
          select new Profile 
           { 
            Name = (string)profiles.Element("Name"), 
            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"), 
            Losses = (string)profiles.Element("Ratio").Element("Loss"), 
            Abandoned = (string)profiles.Element("Ratio").Element("Abandoned") 
           }).FirstOrDefault(); 
     } 

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

私のコードがあるとプロファイルアイテムとクエリは、このエラーが発生する原因となるプロファイルのコレクションを割り当てます。試してみてくださいFirstOrDefault()

+0

ありがとうDotnetstep ...それはそれを修正しました。 – Yecats

+1

'currentProfile = new Profile();'を省略すると、 'Profile'を返さなければなりません。 –

+0

ありがとう@KrisIvanov - 私は最初の行を削除しました。私は混乱しています、なぜそれがプロファイルを返し、currentProfileを返さないといけませんか? – Yecats

関連する問題