不能隐式转换类型'System.Collections.Generic.IEnumerable

本文关键字:System Collections Generic IEnumerable 转换 类型 不能 | 更新日期: 2023-09-27 18:18:41

我收到以下错误:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'Munchkin.Model.PlayerProfiles.Profile'. An explicit conversion exists (are you missing a cast?)
我的代码是:
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")
        });
}

不能隐式转换类型'System.Collections.Generic.IEnumerable

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();
        }

由于当前配置文件是单个配置文件项,并且查询分配配置文件集合,这就是为什么会出现此错误。尝试使用 FirstOrDefault ()

相关文章: