0

What I'm trying to do:

I'm trying to join two diferent tables so I can show them in my view with my view model..

It works fine when I don't have similar Id's

Problem:

Everytime I add a new scholarship and I try to view the list again I get an error message "Object Null Reference Exception".. but if I restart my proyect in visual studio everything works just fine..

Question:

How can I check if my new object is not null??

Anyone knows why I need to restart my proyect in order to view my data??

(I'm using asynchronous & await to call my db's.. don't know if that has anything to do with anything.)

..Thanks in advance!

=> My last attempt...

       var model = students.Join(scholarshipsRequests,
           x => x.ScholarshipId ?? 0, y => y.ScholarshipId,
           (x, y) => x != null && y != null ? (new AdminScholarshipReqViewModel
           {
               StudentId = x.StudentId,
               ScholarshipId = y.ScholarshipId,
               Tag = x.Tag,
               FullName = x.FullName,
               Division = x.Career.Division,
               Company = y.Company,
               Comments = y.Comments,
               CreationDate = y.CreationDate,
               ClassificationDescription = y.Classifications.Description,
           }) : new AdminScholarshipReqViewModel()
           {
               StudentId = 0,
               ScholarshipId = 0,
               Tag = "",
               FullName = "",
               Division = "",
               Company = "",
               Comments = "",
               CreationDate = DateTime.Now,
               ClassificationDescription = "",

           }).ToPagedList(pageNumber, pageSize);
            if (model != null) return View(model);
Luis
  • 1
  • 2
  • You should look at the answers for this question - there's probably a better way to do this using LINQ's navigation properties: http://stackoverflow.com/questions/37324/what-is-the-syntax-for-an-inner-join-in-linq-to-sql – Taraz Feb 19 '16 at 21:22

1 Answers1

0

You could put each group into an IEnumerable, then use:

if (students != null && students.Any() && scholarshipsRequests != null && scholarshipsRequests.Any()) {
}

I think you also need to check for the case where there are no records, not just a null object.

Taraz
  • 1,242
  • 13
  • 13