我有以下LINQ查询 -
var data = (from req in db.tblRequirements
where req.guidRequirementId == guidRequirementId
&& (!db.ViewMIPRAlls.Any(x=>x.guidRequirementId == req.guidRequirementId))
select new
{
guidRequirementId = req.guidRequirementId,
strStatus = req.tblCodesRequirementStatus.strDescription,
strNumber = req.ViewNumber.strAwardNumber,
strOffice = req.tblOrganization.strAcronym,
strCustomerOffice = req.tblOrganizationCustomer.strAcronym,
strDescription = req.strDescription,
strNotes = req.strNotes,
strStatusId = req.strStatusId,
strCompany = req.strCompany,
strUpdatedBy = Person.GetPersonNameFromUserID(req.guidUserId),
dtmUpdated = Requirement.UpdateTime(req.guidRequirementId),
dtmEsitmatedEnd = (req.tblDates.Select(x=>x.dtmEnd) != null
? req.tblDates.Select(x=>x.dtmEnd.ToShortDateString()).ToString()
: ""
)
}).OrderBy(x=>x.dtmEnd);
我无法按日期订购。此查询会导致以下错误:
无法将节点'ClientQuery'格式化为SQL执行。
答案 0 :(得分:4)
在选择之前Orderby,或者在select
中创建的Orderby答案 1 :(得分:3)
如果您尝试按SQL中的字段排序,则OrderBy
子句应位于select new
之前。
您的LINQ语句可以转换为:
var data =
from req in db.tblRequirements
from dates in req.tblDates
where req.guidRequirementId == guidRequirementId
&& (!db.ViewMIPRAlls.Any(x=>x.guidRequirementId == req.guidRequirementId))
order by dates.dtmEnd
select new
{
guidRequirementId = req.guidRequirementId,
strStatus = req.tblCodesRequirementStatus.strDescription,
strNumber = req.ViewNumber.strAwardNumber,
strOffice = req.tblOrganization.strAcronym,
strCustomerOffice = req.tblOrganizationCustomer.strAcronym,
strDescription = req.strDescription,
strNotes = req.strNotes,
strStatusId = req.strStatusId,
strCompany = req.strCompany,
strUpdatedBy = Person.GetPersonNameFromUserID(req.guidUserId),
dtmUpdated = Requirement.UpdateTime(req.guidRequirementId),
dtmEsitmatedEnd = (req.tblDates.Select(x=>x.dtmEnd) != null
? req.tblDates.Select(x=>x.dtmEnd.ToShortDateString()).ToString()
: ""
)
}
请注意,根据tblRequirements
和tblDates
之间的关系类型,这可能会产生比初始选择更多的记录。
为了解决这个问题,您需要考虑一种方法来汇总tblDates
对应于tblRequirements
记录的记录(也许只需要max(tblDates.dtmEnd)
?)
答案 2 :(得分:0)
尝试:
where req.guidRequirementId == guidRequirementId
&& (!db.ViewMIPRAlls.Any(x=>x.guidRequirementId == req.guidRequirementId))
order by req.dtmEnd
select new