2011-12-25 74 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

相关问题