Newbie Newbie - 8 months ago 64
C# Question

Moving an object in list to the front of the list

Newbie here, I have a list


If when going through the list

bool something = true

I want this item to move and now be the first item in the list (so it will show up first.)
I tried this

@foreach (var c in Model.Cleaners)
var item = c;

But I am getting an error Cannot convert type 'void' to object' What does that mean?

Answer Source

I don't know if there is an easier way to do it, but in the past when confronting this problem I did the following.

I added a nullable int to the database table, then I made a foreach loop and depending on whatever qualifications I wanted on top I gave a lower number. Then I did orderby.

The advantage of doing it like this is you have a lot of flexibility in your order. You can give 3 values 0, 1, and 2.


  @foreach (var c in Model.Cleaners)
c.ConfirmationChecker = 1;
foreach (var p in c.TimeConfirmations)
    if (condition)
        c.ConfirmationChecker = 0;


Then afterwards you can do orderby

  @foreach (var c in Model.Cleaners.OrderBy(x => x.ConfirmationChecker))

Hope this helps :)!