我有以下代码:
var expressions = new List<IQueryable<Container>>();
var containers1 = containers
.Where(x => EF.Functions.Like(x.ContainerReference1, $"%{message.SearchValue}%")
|| EF.Functions.Like(x.ContainerReference2, $"%{message.SearchValue}%"))
.OrderBy(x => x.ContainerReference1)
.ThenBy(x => x.ContainerReference2)
.ThenByDescending(x => x.DateUpdated);
expressions.Add(containers1);
var containers2 = containers
.Where(x => EF.Functions.Like(x.Description, $"%{message.SearchValue}%"))
.OrderBy(x => x.Description)
.ThenByDescending(x => x.DateUpdated);
expressions.Add(containers2);
var containers3 = containers.Where(x => x.ContactEmails
.OrderBy(y => y.Email)
.ThenBy(y => y.DisplayName)
.Any(y => EF.Functions.Like(y.Email, $"%{message.SearchValue}%")
|| EF.Functions.Like(y.DisplayName, $"%{message.SearchValue}%")))
.OrderByDescending(x => x.DateUpdated);
expressions.Add(containers3);
var containers4 = containers
.Where(x => EF.Functions.Like(x.Keywords, $"%{message.SearchValue}%"))
.OrderBy(x => x.Keywords)
.ThenByDescending(x => x.DateUpdated);
expressions.Add(containers4);
containers = expressions.Aggregate((acc, i) => acc.Union(i));
但是在.Union
操作之后,排序被重置。如何防止排序重置?
1条答案
按热度按时间zkure5ic1#
Union
运算符不保留元素的顺序。您需要根据数据的存在动态地构造排序逻辑