Use LINQ to move item to top of list

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

    public static IEnumerable ServeFirst(this IEnumerable source, 
        Predicate p)
    {
        var list = new List();
    
        foreach (var s in source)
        {
            if (p(s))
                yield return s;
            else
                list.Add(s);
        }
    
        foreach (var s in list)
            yield return s;
    }
    

提交回复
热议问题