2011-12-25 38 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 2011-12-25 03:36:48

+1

你應該省略這一行'currentProfile = new Profile();',這是完全不必要的,該方法也應該返回'Profile' – 2011-12-25 04:15:02

+0

謝謝@KrisIvanov - 我已經刪除了第一行。我很困惑,但爲什麼它應該返回Profile而不是currentProfile? – Yecats 2011-12-26 00:24:37

相關問題