所以这是我的查询
var results = tcs.Skip(searchModel.PageSize * (searchModel.Page - 1))
.Take(searchModel.PageSize)
.AsEnumerable()
.Select(x => new
{
trackId = x.TrackId,
trackName = x.TrackName,
category = _weCategoryService.FindAll().Where(y => y.WorkExperience_Track.TrackId == x.TrackId)
.Select(y => new {
categoryId = y.CategoryId,
categoryName = y.CategoryName,
skill = _skillsService.FindAll().Where(z => z.CategoryId == y.CategoryId)
.Select(z => new {
skillId = z.SkillId,
skillName = z.SkillName
}).OrderBy(z => z.skillName).ToList()
}).OrderBy(y => y.categoryName).ToList()
}).OrderBy(x => x.trackName).ToList();
然后我有一个模型,该模型具有
SortTrack
,SortCategory
和SortSkills
的布尔值。我想要OrderBy如果布尔值是true,则升序,如果是false,则降序。
如何实现?
最佳答案
在lambda中,可以这样完成:
var results = tcs.Skip(searchModel.PageSize * (searchModel.Page - 1))
.Take(searchModel.PageSize)
.AsEnumerable()
.Select(x => new
{
trackId = x.TrackId,
trackName = x.TrackName,
category = _weCategoryService.FindAll().Where(y => y.WorkExperience_Track.TrackId == x.TrackId)
.Select(y => new {
categoryId = y.CategoryId,
categoryName = y.CategoryName,
skill = _skillsService.FindAll().Where(z => z.CategoryId == y.CategoryId)
.Select(z => new {
skillId = z.SkillId,
skillName = z.SkillName
}).OrderBy(z => SortSkills ? z.skillName : "").OrderByDescending(z => !SortSkills ? z.skillName : "").ToList()
}).OrderBy(y => SortCategory ? y.categoryName : "").OrderByDescending(y => !SortCategory ? y.categoryName : "").ToList()
}).OrderBy(x => SortTrack ? x.trackName : "").OrderByDescending(x => !SortTrack ? x.trackName : "").ToList();
否则,您需要使用以下表达式:
var x = widgets.Where(w => w.Name.Contains("xyz"));
if (flag) {
x = x.OrderBy(w => w.property);
} else {
x = x.OrderByDescending(w => w.property);
}
关于c# - C#LinQ条件订单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41561844/