当我返回多行时,我正试图从表tbBreadCrumb中选择BreadCrumbID的最大值。现在我将tabel tbBreadCrumb加入到表tbProjects中。如果tbBreadCrumb中有多个条目,则返回多个条目。但我只想在tbBreadCrumb中选择具有最高BreadCrumbID的行。
这是我的代码:
using (dbPSREntities5 myEntities = new dbPSREntities5())
{
var allDepartments = (from tbProject in myEntities.tbProjects
from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID) <--- I want to put some sort of Max() function here where it will only slect the row with the max BreadCrumID in the table tbBreadCrumb
from refBreadCrumb in myEntities.refBreadCrumbs.Where(x => x.refBreadCrumbID == tbBreadCrumb.StatusID)
// select new anon type
select new
{
ProjectID = tbProject.ProjectID,
Status = refBreadCrumb.BreadCrumbValue,
DateSubmitted = tbBreadCrumb.CreateDateTime,
refDepartmentID = tbProject.refDepartmentID,
ProjectContactFullName = tbProject.ProjectContactFirstName + " " + tbProject.ProjectContactLastName,
ProjectWorkType = tbProject.ProjectWorkType,
});
projectsListView.DataSource = allDepartments;
projectsListView.DataBind();
}
答案 0 :(得分:1)
您可以尝试此操作,首先获取最高BreadCrumbID
ID。然后在第二个查询中将其与where
一起使用:
var breadId = myEntities.tbBreadCrumbs.Max(x => x.BreadCrumbID);
var allDepartments = (from tbProject in myEntities.tbProjects
from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID && x.BreadCrumbID == breadId)
from refBreadCrumb in myEntities.refBreadCrumbs.Where(x => x.refBreadCrumbID == tbBreadCrumb.StatusID)
// select new anon type
select new
{
ProjectID = tbProject.ProjectID,
Status = refBreadCrumb.BreadCrumbValue,
DateSubmitted = tbBreadCrumb.CreateDateTime,
refDepartmentID = tbProject.refDepartmentID,
ProjectContactFullName = tbProject.ProjectContactFirstName + " " + tbProject.ProjectContactLastName,
ProjectWorkType = tbProject.ProjectWorkType,
});
更新:
var allDepartments = (from tbProject in myEntities.tbProjects
from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID && x.BreadCrumbID == myEntities.tbBreadCrumbs.Where(y => y.ProjectID == tbProject.ProjectID).Max(y => y.BreadCrumbID))
from refBreadCrumb in myEntities.refBreadCrumbs.Where(x => x.refBreadCrumbID == tbBreadCrumb.StatusID)
// select new anon type
select new
{
ProjectID = tbProject.ProjectID,
Status = refBreadCrumb.BreadCrumbValue,
DateSubmitted = tbBreadCrumb.CreateDateTime,
refDepartmentID = tbProject.refDepartmentID,
ProjectContactFullName = tbProject.ProjectContactFirstName + " " + tbProject.ProjectContactLastName,
ProjectWorkType = tbProject.ProjectWorkType,
});