我有这个查询但是我需要添加一个 where 子句,它使用'Contains()'所以我可以检查数据库中的accountID列表,我需要这样做的原因是因为我必须首先检查是否有任何accountID,如果有必须执行where子句中的'Contains()'但是编译器会抱怨如果我尝试这样做但是考虑到方法必须做什么我可以考虑到在查询中加入了如此多的表这一事实,找出了另一种方法。
我的代码:
bool UseAcc = false;
if (accountIds != null && accountIds.Count > 0)
{
UseAcc = true;
}
var query = (from i in db.Incidents
join s in db.Sites on i.SiteID equals s.SiteID
join a in db.Accounts on i.AccountID equals a.AccountID
join st in db.Status on i.StatusID equals st.StatusID
join currentUser in db.Users on i.CurrentAssignedUser equals currentUser.UserID
join loggedByUser in db.Users on i.LoggedByUser equals loggedByUser.UserID
join createdByUser in db.Users on i.CreatedByUser equals createdByUser.UserID
join l in db.Locations on i.Location equals l.LocationID into locList
from loc in locList.DefaultIfEmpty()
join q in db.QuestionCategories on i.QuestionCategoryID equals q.QuestionCategoryID
join ia in db.IncidentActions on i.IncidentID equals ia.IncidentID into iaList
from actions in iaList.DefaultIfEmpty()
//If 'UseAcc' gets set to true then execute the contains section of the or statement
where (i.Active == true) &&
(UseAcc = false || a => accountIds.Contains(a.AccountID))
select new
{
Title = i.Title,
IncidentID = i.IncidentID,
StatusName = st.StatusName,
StatusID = i.StatusID,
ReferenceNumber = i.ReferenceNo,
AccountName = a.AccountName,
AccountID = i.AccountID,
SiteName = s.SiteName,
SiteID = i.SiteID,
LocationName = loc.LocationName,
LocationID = i.Location,
CatName = q.QuestionCategoryName,
CatID = i.QuestionCategoryID,
CurrentAssignedUser = currentUser.FirstName + " " + currentUser.LastName,
AssignedUserID = i.CurrentAssignedUser,
CreatedByUser = createdByUser.FirstName + " " + createdByUser.LastName,
DateCreated = i.LoggedDate,
DepartmentID = i.DepartmentID,
Logger = loggedByUser.FirstName + " " + loggedByUser.LastName,
LoggedBy = i.LoggedByUser,
EscalationCount = i.EscalationCount)
});
我会粘贴整个方法,但它真的很大。
我已经在线查看了示例,但它们也无法工作,因为很多人使用带有单个表的AsQueryable,并且此查询已连接了许多表。
请帮助。
答案 0 :(得分:1)
var accounts = db.Accounts.AsQueryable();
if (accountIds != null && accountIds.Count > 0)
{
accounts = accounts.Where(a => accountIds.Contains(a.AccountID));
}
var query = (from i in db.Incidents
join s in db.Sites on i.SiteID equals s.SiteID
join a in accounts on i.AccountID equals a.AccountID
join st in db.Status on i.StatusID equals st.StatusID
join currentUser in db.Users on i.CurrentAssignedUser equals currentUser.UserID
join loggedByUser in db.Users on i.LoggedByUser equals loggedByUser.UserID
join createdByUser in db.Users on i.CreatedByUser equals createdByUser.UserID
join l in db.Locations on i.Location equals l.LocationID into locList
from loc in locList.DefaultIfEmpty()
join q in db.QuestionCategories on i.QuestionCategoryID equals q.QuestionCategoryID
join ia in db.IncidentActions on i.IncidentID equals ia.IncidentID into iaList
from actions in iaList.DefaultIfEmpty()
select new
{
Title = i.Title,
IncidentID = i.IncidentID,
StatusName = st.StatusName,
StatusID = i.StatusID,
ReferenceNumber = i.ReferenceNo,
AccountName = a.AccountName,
AccountID = i.AccountID,
SiteName = s.SiteName,
SiteID = i.SiteID,
LocationName = loc.LocationName,
LocationID = i.Location,
CatName = q.QuestionCategoryName,
CatID = i.QuestionCategoryID,
CurrentAssignedUser = currentUser.FirstName + " " + currentUser.LastName,
AssignedUserID = i.CurrentAssignedUser,
CreatedByUser = createdByUser.FirstName + " " + createdByUser.LastName,
DateCreated = i.LoggedDate,
DepartmentID = i.DepartmentID,
Logger = loggedByUser.FirstName + " " + loggedByUser.LastName,
LoggedBy = i.LoggedByUser,
EscalationCount = i.EscalationCount)
});