【问题标题】:Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerableLinq - 无法隐式转换类型'System.Collections.Generic.IEnumerable
【发布时间】:2013-10-07 13:58:45
【问题描述】:

我收到以下错误:

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

【问题讨论】:

    标签: c# linq error-handling implicit-conversion


    【解决方案1】:
    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();
            }
    

    由于您的 currentProfile 是单个配置文件项,因此查询分配配置文件集合,这就是出现此错误的原因。尝试使用 FirstOrDefault()

    【讨论】:

    • 感谢 Dotnetstep... 修复了它。
    • 你应该省略这行currentProfile = new Profile();,完全没有必要,该方法也应该返回Profile
    • 谢谢@KrisIvanov - 我已经删除了第一行。我很困惑,为什么它应该返回 Profile 而不是 currentProfile?
    • 我的建议是删除currentProfile = new Profile(); 行,因为Profile 的创建会在下一行被覆盖
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多