我想按开始日期(s.StartDate)订购。下面是我的代码到目前为止,我的理解是我应该在某个地方添加.orderby(s.StartDate)
,但我认为我现在甚至没有采取正确的路线,因为我尝试了很多方法。
var query = from s in context.SessionSearch
where s.Children == 0 && s.IsPublic == isPublic
select s;
var query = from s in context.SessionSearch
where s.Children == 0 && s.IsPublic == isPublic
if (startDate != null)
{
query = query.Where(s => s.StartDate >= startDate && s.StartDate <= endDate);
}
答案 0 :(得分:0)
你应该可以从&#34开始;没有startdate&#34;选项 - 这里有几个选项 - 要么专门声明查询的类型:
IQueryable<SessionSearch> query = from s in context.SessionSearch
where s.Children == 0 && s.IsPublic == isPublic
order by s.StartDate
select s;
然后,当您尝试过时,如果传入了开始日期,请在查询中添加其他where
子句:
query = query.Where(s => s.StartDate >= startDate && s.StartDate <= endDate);
这意味着您无法通过ThenBy
方法添加进一步的排序。
或者,您可以在添加where子句后添加order by
子句:
var query = from s in context.SessionSearch
where s.Children == 0 && s.IsPublic == isPublic
select s;
if (startDate != null) {
query = query.Where(s => s.StartDate >= startDate && s.StartDate <= endDate);
}
query = query.OrderBy(s => s.StartDate);