简体   繁体   中英

ASP.NET MVC5 - (Entity Framework) LINQ Query Join & Where returning only 1 result but not other result if changed

Okay. I will describe to the best of my abilities what I am trying to accomplish. I could not find any Stack Overflow questions that poses my question.

ASP.NET MVC5 - (Entity Framework) LINQ Query from DBContext Models into ViewModel

I am using ASP.NET MVC5 with Entity Framework and I have scaffolded the DBContext into the class models Schools and Contacts . I am trying to use LINQ Query to join the two models together and assign it into the ViewModel called SchoolVM .

I joined Contacts with Schools through the school.ID equals contact.School_ID

So for the record id = 1 in Schools , it does return the result that I wanted.

But when I re-query for id = 2 or id = 3 and so forth, it returns nothing, with a count of zero.

Just to let everyone know, the records for id 2,3,4 and so on does exist.

Can anybody help me with this? Let me know if you need more code information.

Here is the Controller Action Method down below.

public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    //**LINQ Query joining two Models and putting into a ViewModel.
    //**LINQ Query that seems to return the record ID == 1, but not
    //**existing record ID == 2 or 3 or so on and so forth.
    var schoolDetails = (from school in db.Schools
                         join contact in db.Contacts on school.ID equals contact.School_ID
                         where school.ID == id
                         select new { school, contact }).ToList();

    //**This is the ViewModel that I am trying to assign the joined LINQ query into.
    SchoolVM schoolVM = new SchoolVM();

    foreach (var value in schoolDetails)
    {

        //**Schools Model Information
        schoolVM.ID = value.school.ID;
        schoolVM.SchoolName = value.school.SchoolName;
        schoolVM.Address = value.school.Address;
        schoolVM.City = value.school.City;
        schoolVM.State = value.school.State;
        schoolVM.Zip = value.school.Zip;
        schoolVM.MainPhone = String.IsNullOrEmpty(value.school.MainPhone) ? "" : String.Format("{0:(###) ###-####}", double.Parse(value.school.MainPhone));
        schoolVM.Website = value.school.Website;
        schoolVM.NumberOfStudents = value.school.NumberOfStudents;
        schoolVM.SchoolOrDistrict = value.school.SchoolOrDistrict;
        schoolVM.CountyID = value.school.CountyID;

        //**Contacts Model Information
        schoolVM.ContactFirstName = value.contact.ContactFirstName;
        schoolVM.ContactLastName = value.contact.ContactLastName;
        schoolVM.ContactTitle = value.contact.ContactTitle;
        schoolVM.ContactPhone = value.contact.ContactPhone;
        schoolVM.ContactEmail = value.contact.ContactEmail;
        schoolVM.PrimaryOrSecondary = value.contact.PrimaryOrSecondary;
        schoolVM.Coordinator = value.contact.Coordinator;
    }

    //**This is where LINQ is returning COUNT = 0 for ID = 2, 3, 4 and so forth.
    if (schoolDetails == null || schoolDetails.Count == 0)
    {
        return HttpNotFound();
    }

    return View(schoolVM);
}

UPDATE:

Thanks to Daniel Lorenz's input. Very simple solution. I just changed the LINQ Query from INNER JOIN or JOIN to LEFT JOIN.

ORIGINAL:

var schoolDetails = (from school in db.Schools
                     join contact in db.Contacts on school.ID equals contact.School_ID
                     where school.ID == id
                     select new { school, contact }).ToList();

SOLUTION:

//**Changed alias name around a bit to accommodate the rest of the code.

var schoolDetails = (from school in db.Schools
                     join contacts in db.Contacts on school.ID equals contacts.School_ID into schoolvm
                     from contact in schoolvm.DefaultIfEmpty()
                     where school.ID == id
                     select new { school, contact }).ToList();

You are inner joining to contact. Thus if there is no contact for schools with Id 2, 3, you won't get those records back. You need to do a left join instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM