简体   繁体   English

Linq查询不会让我按日期订购

[英]Linq query will not let me order by a date

I have the following LINQ query - 我有以下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);

and I am unable to order it by date. 我无法按日期订购。 This query results in the following error: 此查询导致以下错误:

Could not format node 'ClientQuery' for execution as SQL. 无法格式化节点“ ClientQuery”以执行为SQL。

选择之前的Orderby或通过选择中创建的东西进行Orderby

The OrderBy clause should come before the select new if you try to sort by fields in SQL. 如果尝试按SQL中的字段排序,则OrderBy子句应位于select new之前。

Your LINQ statement could be transformed in: 您的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()
            : ""
        )
    }

Please note that depending on the type of relationship between tblRequirements and tblDates , this may yield more records than your initial select. 请注意,根据tblRequirementstblDates之间关系的类型,这可能会产生比初始选择更多的记录。 To overcome this, you'll need to think of a way to aggregate the records in tblDates corresponding to a tblRequirements record (perhaps just take max(tblDates.dtmEnd) ?) 为了克服这个问题,您需要考虑一种在tblDatestblDatestblRequirements记录相对应的记录的方法(也许只取max(tblDates.dtmEnd)吗?)

Try: 尝试:

where req.guidRequirementId == guidRequirementId
        && (!db.ViewMIPRAlls.Any(x=>x.guidRequirementId == req.guidRequirementId))
order by req.dtmEnd     
select new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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