Use LINQ to move item to top of list

前端 未结 11 1413
轻奢々
轻奢々 2020-12-02 12:00

Is there a way to move an item of say id=10 as the first item in a list using LINQ?

Item A - id =5
Item B - id = 10
Item C - id =12
Item D - id =1

In th

11条回答
  •  情书的邮戳
    2020-12-02 12:33

    You can "group by" in two groups with Boolean key, and then sort them

    var finalList= allCountries
                    .GroupBy(x => x.id != 592)
                    .OrderBy(g => g.Key)
                    .SelectMany(g => g.OrderBy(x=> x.id ));
    

提交回复
热议问题