【问题标题】:Enable null values when assigning one collection to other in asp.net mvc在 asp.net mvc 中将一个集合分配给另一个集合时启用空值
【发布时间】:2015-11-26 11:33:36
【问题描述】:

我写信给以下控制器以将一个集合分配给另一个集合

    public ActionResult Discussion_List() {

        var discussions = (from D in db.AB_Discussion
                           select new
                           {

                           Discussion_ID = D.Discussion_ID,
                           Discussion_Name = D.Discussion_Name,
                           CreatedBy = D.CreatedBy,
                           CreatedDate = D.CreatedDate,
                           UpdatedBy  =  D.UpdatedBy,
                           UpdatedDate =  D.UpdatedDate

                           }).ToList();

        var models = discussions.Select(b => new Discussion_Dashboard()

        {
            Discussion_ID = b.Discussion_ID,
            Discussion_Name = b.Discussion_Name,
            CreatedBy = db.AspNetUsers.Find(b.CreatedBy).UserName,
            CreatedDate = b.CreatedDate,
            UpdatedBy = db.AspNetUsers.Find(b.UpdatedBy).UserName,
            UpdatedDate = b.UpdatedDate

        });

        return View(models);
    }

但是当我在数据库表中的上述任何一个字段中有空值时 AB_Discussion 我收到以下错误

对象引用未设置为对象的实例。

如何避免这个错误

这是我的相关模型类

   public partial class Discussion_Dashboard
    {
        public int Discussion_ID { get; set; }
        public string Discussion_Name { get; set; }
        public string Discription { get; set; }
        public string CreatedBy { get; set; }
        public Nullable<System.DateTime> CreatedDate { get; set; }
        public string UpdatedBy { get; set; }
        public Nullable<System.DateTime> UpdatedDate { get; set; }
        public int Views { get; set; }

        public int replys { get; set; }

    }

   public partial class AB_Discussion
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Discussion_ID { get; set; }
        public string Discussion_Name { get; set; }
        public string Discription { get; set; }
        public string CreatedBy { get; set; }
        public Nullable<System.DateTime> CreatedDate { get; set; }
        public string UpdatedBy { get; set; }
        public Nullable<System.DateTime> UpdatedDate { get; set; }
    }

【问题讨论】:

  • 为什么要创建第一个匿名对象集合? - 可以是var discussions = (from D in db.AB_Discussion select D).ToList();
  • 因为我想通过 ID 查找用户名,所以我将其传递给另一个
  • 您刚刚创建了另一个与您现有收藏相同的收藏(毫无意义)
  • 但是我怎样才能首先找到用户名?
  • UpdatedBy = b.UpdatedBy == null ? null : db.AspNetUsers.Find(b.UpdatedBy).UserName,

标签: c# asp.net asp.net-mvc linq asp.net-mvc-3


【解决方案1】:

我建议使用映射器,例如您可以在 GitHub 上找到或通过 NuGet 安装的 AutoMapper。 AutoMapper 可帮助您以相对简单的方式将一个类映射到另一个类,并为具有相同名称的字段提供合理的默认值等。

如果你想允许空值,你可以使用 ?变量声明中类型之后的运算符,例如:

public int? views { get; set; }

这将为您的变量提供一个公共的只读属性 HasValue,您可以使用它来查看您的变量是否包含值。

【讨论】:

  • 字符串的语法是什么?
  • 语法一样,只是替换'int?'用“字符串?”
猜你喜欢
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多