ASP.NET MVC5 - (实体框架)LINQ查询加入&返回仅返回1个结果,但如果更改则不返回其他结果

时间:2017-09-20 18:00:51

标签: c# asp.net-mvc entity-framework linq viewmodel

好。我将尽我所能描述我想要完成的事情。我找不到任何提出问题的Stack Overflow问题。

ASP.NET MVC5 - (实体框架)从DBContext模型到ViewModel的LINQ查询

我正在使用带有Entity Framework的ASP.NET MVC5,我已将DBContext搭建到类模型 SchoolsContacts中。我正在尝试使用LINQ Query将两个模型连接在一起,并将其分配到名为SchoolVM ViewModel

我通过Contacts

加入了Schools school.ID equals contact.School_ID

因此对于id = 1中的记录Schools,它会返回我想要的结果。

但是当我重新查询id = 2id = 3等等时,它不返回任何内容,计数为零。

只是为了让每个人都知道,id 2,3,4等的记录确实存在。

有人可以帮我这个吗?如果您需要更多代码信息,请与我们联系。

以下是控制器操作方法。

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

更新

感谢Daniel Lorenz的投入。很简单的解决方案我只是将LINQ查询从INNER JOIN或JOIN更改为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();

1 个答案:

答案 0 :(得分:2)

你内心加入联系。因此,如果没有与Id 2,3的学校联系,您将无法获得这些记录。你需要做一个左连接。