Use LINQ to move item to top of list

前端 未结 11 1394
轻奢々
轻奢々 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:24

    To also check if the item was found without Exception, something like:

    var allCountries = repository.GetCountries();
    var lookup = allCountries.ToLookup(x => x.id == 592);  
    var finalList = lookup[true].Concat(lookup[false]).ToList();
    if ( lookup[true].Count() != 1 ) YouAreInTrouble();
    

提交回复
热议问题