简体   繁体   中英

LINQ - OrderByDescending

Please note: As of 1:47 PM UTC, Aug 26 2016, I completely modified this question because originally it was very confusing. My fault for that. So please ignore all answers/comments before this time. Thanks

I have a list of items that get added to a SearchItems list by SearchType... SearchType of ObjectAType, ObjectBType etc.. I want to OrderByDescending SearchTimes by items that have SearchType of ObjectAType or ObjectBType. For all other SearchType it needs to be order by ascending.

This is my query:

var orderdItems = SearchItems.OrderBy(i => i.ItemDisplay)
                             .OrderBy(i => i.CategoryDisplay))

This is the criteria I want to order by descending.

Where(x => x.SearchType == ObjectAType || x.SearchType == ObjectBType)

This is the structure of SearchItem

public class SearchItem
{
    public SearchItem() { }
    public Guid ItemId { get; set; }
    public string ItemDisplay { get; set; }
    public string CategoryDisplay { get; set; }
    public string SearchType { get; set; }
}

And this is how they get added to SearchItems = List<SearchItems>();

SearchItems.AddRange(items.Select(i => new SearchItem()
            {
                CategoryDisplay = "Object A",
                ItemDisplay = i.ObjectADisplay,
                ItemId = i.ObjectAId,
                SearchType = ObjectAType,
            }));

SearchItems.AddRange(items.Select(i => new SearchItem()
            {
                CategoryDisplay = "Object B",
                ItemDisplay = i.ObjectBDisplay,
                ItemId = i.ObjectBId,
                SearchType = ObjectBType,
            }));

and so on....'items' are IEnumerable<ObjectA>, IEnumerable<ObjectB> etc..

var desc = this.SearchItems
               .Where(x => x.SearchType == Foo || x.SearchType == Bar)
               .OrderByDescending(i => i.ItemDisplay)
               .ThenByDescending(i => i.CategoryDisplay);
var asc = this.SearchItems
              .Where(x => x.SearchType != Foo && x.SearchType != Bar)
              .OrderBy(i => i.ItemDisplay)
              .ThenBy(i => i.CategoryDisplay);
var result = desc.Concat(asc);

Of course you can do it as one query as well.

I assume that you want the 2 descending objects first. If that's not the case then turn the last statement around.

var result = asc.Concat(desc);

If you want to sort the list itself, then do this

this.SearchItems = desc.Concat(asc).ToList();

The question is rather confusing, but if I've understood correctly you've got 26 objects and you want a serialized list of those objects such that Object A is first, then Object B, then all remaining objects ordered by Property C.

If so, that can be written by ordering by the object being Object A first.

SearchItemsJson = serializer.Serialize(
                    this.SearchItems
                        .OrderByDescending(i => i.SearchType == "Foo")
                        .ThenByDescending(i => i.SearchType == "Bar")
                        .ThenBy(i => i.ItemDisplay)
                        .ThenBy(i => i.CategoryDisplay))

Having said that, it sounds like you hardcoding some logic about the objects that probably belongs as a property of the object. What is special about these two objects that mean they need to be listed first. Might you have a third special object in future? Adding a flag such as "Featured" to the object may be a more flexible way of modelling the business problem you are trying to solve.

SearchItems.OrderBy(x => (x.SearchType == Foo || x.SearchType == Bar) ? 0 : 1);

That will result with SearchType = Foo or Bar at the top of the list and everything else after it. You can get more specific if you want Foo before Bar with:

SearchItems.OrderBy(x => x.SearchType == Foo ? 0 : x.SearchType == Bar ? 1 : 2);

In your example I have no idea what SearchType is or Foo or Bar but I think you get the idea.

In this example the type that will be used for sorting is int and default sorting is Ascending (small to large). So the OrderBy expression above will return 0 for Foo placing it first, 1 for Bar placing it 2nd, and 2 for anything else placing those after Foo and Bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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