C# IQueryable .Union 重置排序

huangapple go评论59阅读模式
英文:

C# IQueryable .Union reset sorting

问题

但是在 .Union 操作之后,排序被重置了。
如何防止排序被重置?

英文:

I have the following code:

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));

But after .Union operation sorting is reset.
How can I prevent resetting of sorting?

答案1

得分: 1

Union 操作符不会保留元素的顺序。您需要根据数据的存在动态构建排序逻辑。

var expressions = new List<IQueryable<Container>>();
var sortingExpressions = new List<Func<IQueryable<Container>, IOrderedQueryable<Container>>>();

var containers1 = containers
    .Where(x => EF.Functions.Like(x.ContainerReference1, $"%{message.SearchValue}%")
                || EF.Functions.Like(x.ContainerReference2, $"%{message.SearchValue}%"));
if (containers1.Any())
{
    var containers1Sorting = new Func<IQueryable<Container>, IOrderedQueryable<Container>>(x => x
        .OrderBy(y => y.ContainerReference1)
        .ThenBy(y => y.ContainerReference2)
        .ThenByDescending(y => y.DateUpdated));

    expressions.Add(containers1);
    sortingExpressions.Add(containers1Sorting);
}

var containers2 = containers
    .Where(x => EF.Functions.Like(x.Description, $"%{message.SearchValue}%"));
if (containers2.Any())
{
    var containers2Sorting = new Func<IQueryable<Container>, IOrderedQueryable<Container>>(x => x
        .OrderBy(y => y.Description)
        .ThenByDescending(y => y.DateUpdated));

    expressions.Add(containers2);
    sortingExpressions.Add(containers2Sorting);
}

var containers3 = containers
    .Where(x => x.ContactEmails
        .Any(y => EF.Functions.Like(y.Email, $"%{message.SearchValue}%")
            || EF.Functions.Like(y.DisplayName, $"%{message.SearchValue}%")));
if (containers3.Any())
{
    var containers3Sorting = new Func<IQueryable<Container>, IOrderedQueryable<Container>>(x => x
        .OrderBy(y => y.ContactEmails.OrderBy(z => z.Email).ThenBy(z => z.DisplayName))
        .OrderByDescending(x => x.DateUpdated));

    expressions.Add(containers3);
    sortingExpressions.Add(containers3Sorting);
}

var containers4 = containers
    .Where(x => EF.Functions.Like(x.Keywords, $"%{message.SearchValue}%"));
if (containers4.Any())
{
    var containers4Sorting = new Func<IQueryable<Container>, IOrderedQueryable<Container>>(x => x
        .OrderBy(y => y.Keywords)
        .ThenByDescending(y => y.DateUpdated));

    expressions.Add(containers4);
    sortingExpressions.Add(containers4Sorting);
}

var mergedContainers = expressions.Aggregate((acc, i) => acc.Union(i));

if (sortingExpressions.Any())
{
    var mergedSorting = sortingExpressions
        .Aggregate((acc, next) => q => next(acc(q)));

    containers = mergedSorting(mergedContainers);
}
else
{
    containers = mergedContainers.OrderByDescending(x => x.DateUpdated);
}
英文:

Union operator does not preserve the order of the elements. You need to dynamically construct the sorting logic based on the presence of data

var expressions = new List&lt;IQueryable&lt;Container&gt;&gt;();
var sortingExpressions = new List&lt;Func&lt;IQueryable&lt;Container&gt;, IOrderedQueryable&lt;Container&gt;&gt;&gt;();

var containers1 = containers
        .Where(x =&gt; EF.Functions.Like(x.ContainerReference1, $&quot;%{message.SearchValue}%&quot;)
                || EF.Functions.Like(x.ContainerReference2, $&quot;%{message.SearchValue}%&quot;));
if (containers1.Any())
{
    var containers1Sorting = new Func&lt;IQueryable&lt;Container&gt;, IOrderedQueryable&lt;Container&gt;&gt;(x =&gt; x
        .OrderBy(y =&gt; y.ContainerReference1)
        .ThenBy(y =&gt; y.ContainerReference2)
        .ThenByDescending(y =&gt; y.DateUpdated));

    expressions.Add(containers1);
    sortingExpressions.Add(containers1Sorting);
}

var containers2 = containers
          .Where(x =&gt; EF.Functions.Like(x.Description, $&quot;%{message.SearchValue}%&quot;));
if (containers2.Any())
{
    var containers2Sorting = new Func&lt;IQueryable&lt;Container&gt;, IOrderedQueryable&lt;Container&gt;&gt;(x =&gt; x
        .OrderBy(y =&gt; y.Description)
        .ThenByDescending(y =&gt; y.DateUpdated));

    expressions.Add(containers2);
    sortingExpressions.Add(containers2Sorting);
}

var containers3 = containers
        .Where(x =&gt; x.ContactEmails
            .Any(y =&gt; EF.Functions.Like(y.Email, $&quot;%{message.SearchValue}%&quot;)
                || EF.Functions.Like(y.DisplayName, $&quot;%{message.SearchValue}%&quot;)));
if (containers3.Any())
{
    var containers3Sorting = new Func&lt;IQueryable&lt;Container&gt;, IOrderedQueryable&lt;Container&gt;&gt;(x =&gt; x
        .OrderBy(y =&gt; y.ContactEmails.OrderBy(z =&gt; z.Email).ThenBy(z =&gt; z.DisplayName))
        .OrderByDescending(x =&gt; x.DateUpdated));

    expressions.Add(containers3);
    sortingExpressions.Add(containers3Sorting);
}

var containers4 = containers
      .Where(x =&gt; EF.Functions.Like(x.Keywords, $&quot;%{message.SearchValue}%&quot;));
if (containers4.Any())
{
    var containers4Sorting = new Func&lt;IQueryable&lt;Container&gt;, IOrderedQueryable&lt;Container&gt;&gt;(x =&gt; x
        .OrderBy(y =&gt; y.Keywords)
        .ThenByDescending(y =&gt; y.DateUpdated));

    expressions.Add(containers4);
    sortingExpressions.Add(containers4Sorting);
}

var mergedContainers = expressions.Aggregate((acc, i) =&gt; acc.Union(i));

if (sortingExpressions.Any())
{
    var mergedSorting = sortingExpressions
        .Aggregate((acc, next) =&gt; q =&gt; next(acc(q)));

    containers = mergedSorting(mergedContainers);
}
else
{
    containers = mergedContainers.OrderByDescending(x =&gt; x.DateUpdated);
}

huangapple
  • 本文由 发表于 2023年6月1日 19:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定