【问题标题】:Cannot implicitly convert type 'int?' to 'system.collections.generic.list <int>?不能隐式转换类型“int?”到'system.collections.generic.list <int>?
【发布时间】:2012-09-28 11:35:14
【问题描述】:

我收到错误消息

不能隐式转换类型'int?'到'system.collections.generic.list?

有人可以帮我解决这个问题吗?

internal class PatchDetails
{
    internal List<int> ServerID { get; set; }
    internal List<string> PatchStatus { get; set; }
}
class Program
{        
    void PatchQueue()
    {
        List<PatchDetails> patchinfo = new List<PatchDetails>();            
        using (PatchEntities server = new PatchEntities())
        {
            patchinfo = (from patches in server.AppPM_Patches
                         where patches.PatchStatus == "Started" 
                         || patches.PatchStatus == "NotStarted"
                         select new PatchDetails { ServerID = patches.ServerId, PatchStatus = patches.PatchStatus }).ToList();
        }
    }
}       

【问题讨论】:

  • PatchStatus 必须同时为“已启动”和“未启动”?
  • 对不起。我更改了代码。现在检查
  • 注意:声明变量时不需要创建列表,因为您会创建另一个列表来分配给它。只需声明变量:List&lt;PatchDetails&gt; patchinfo;

标签: c#-4.0 entity-framework-4


【解决方案1】:

您将int 和string 放入对象中,所以我认为您希望每个PatchDetails 包含它们,而不是它们的列表:

internal class PatchDetails
{
  internal int? ServerID { get; set; }
  internal string PatchStatus { get; set; }
}

如果您不希望ServerID 属性可以为空,则在查询中从int? 中获取值:

ServerID = patches.ServerId.Value

【讨论】:

  • 没有。我想要一个所有 ServerID 和补丁状态的列表
  • @Karthi:你已经有了一个列表。如果该类包含列表,那么您将获得详细信息列表而不是详细信息列表。
【解决方案2】:

select 语句返回单个 PatchDetails 对象,您尝试将单个类强制转换为不正确的列表。

如果您尝试获取单个对象,您可以删除 .ToList()。

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 2011-12-24
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2022-11-17
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多