0

I wrote to following controller to assign one collection to another

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

but when I have null values in any one of above field in database table AB_Discussion I'm getting following error

Object reference not set to an instance of an object.

How to avoid this error

this is my related model classes

   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; }
    }
8
  • Why are you creating the first collection of anonymous objects? - it can just be var discussions = (from D in db.AB_Discussion select D).ToList(); Commented Sep 1, 2015 at 9:34
  • since I want to find username by ID i pass this to another Commented Sep 1, 2015 at 9:35
  • Your just creating another collection which is identical to your existing collection (its pointless) Commented Sep 1, 2015 at 9:37
  • but then How can I find username in firstplace ? Commented Sep 1, 2015 at 9:40
  • 1
    UpdatedBy = b.UpdatedBy == null ? null : db.AspNetUsers.Find(b.UpdatedBy).UserName, Commented Sep 1, 2015 at 10:37

1 Answer 1

0

I would advice to use a mapper, for example the AutoMapper that you can find on GitHub or install via NuGet. The AutoMapper helps you map one class to another in a relatively easy way with reasonable defaults for fields with the same name and so on.

If you want to allow null values you can just define that using the ? operator right after the type in your variable declaration, for example:

public int? views { get; set; }

This will give your variable a public readonly property HasValue that you can use to see whether your variable contains a value or not.

Sign up to request clarification or add additional context in comments.

2 Comments

what the syntax for strings ?
The syntax is the same, just replace 'int?' with 'string?'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.