I have below code -
var refNosToOrder = new int[9] {1,2,3,4,5,6,7,8,9}
var orderedList = lst.OrderBy(x=>x.RefNo==7)
.ThenBy(y=> refNosToOrder.Contains(y.RefNo)).ToList();
lst is list of class object containing int property - RefNo : i.e. List<SampleClass>
class SampleClass
{
public int RefNo {get;set;}
}
lst contains all the unsorted data of RefNo:
lst = 2,4,6,9,7,5,8,1,3
What I want to do -
First I want to order lst by keeping first element as - 7; then for the rest of the list, it should be ordered as the array refNosToOrder
i.e. Final output I am expecting to be -
7,1,2,3,4,5,6,8,9
With the above code -
var orderedList = lst.OrderBy(x=>x.RefNo==7)
.ThenBy(y=> refNosToOrder.Contains(y.RefNo)).ToList();
It is giving - 2,4,6,9,7,5,8,1,3 i.e. this code is not at all ordering the list.
ThenBy()contain somethin likeThenBy(y => y.RefNo)? Look at this post where I saw thatrefNosToOrderis6, 6, 6, 6? Would you want four copies of the original? IsrefNosToOrderalways going to be (effectively) a number followed by a linear sequence that doesn't have that number?