Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Linq OrderBy Not Sort Original Collection?

I am using Linq To Sql as my database layer for the first time and I have run into an issue. Basically, I have a form that allows users to create predefined jobs. A predefined job can have many predefined job items. For example, a predefined job might be something like an oil change. The predefined job items would be oil, labor, etc. In my database PredefinedJobs is a table and PredefinedJobItems is another table with a foreign key back to PredefinedJobs. I have a form for adding predefined jobs that has the Linq-to-Sql class backing the form as a class variable. There is a ListView on the form that displays all of the jobs items. A new feature has required me to track the position of an item in the ListView. For example, if my item ListView looks like below, note the order:

Qty          Name          Desc          ItemOrder
4            Oil           Penzoil       0
1            Labor                       1

Because the items are added via a child form I do not want to provide access to the ListView. So, I created the method below in an attempt to both create the ItemOrder and sort the collection on the PredefinedJob Linq to Sql object. It does not appear that the OrderBy function on the List actually sorts the collection on the PredefinedJob. What would be the best way to maintain order on the Linq to Sql collection (i.e. PredefinedJob.fkJobItems)? Or, would it be better to just pass a reference to my ListView into the child form that adds the items to the jobs where I have access to the selectedIndex?

    private SortAndOrderItems(List<PredefinedJobsItem> items)
    {
        var nextItemOrderNumber = items.Max(max => max.ItemOrder) + 1;
        foreach (var item in items)
        {
            if (item.ItemOrder == null)
            {
                item.ItemOrder = nextItemOrderNumber;
                nextItemOrderNumber++;
            }
        }
        items.OrderBy(i => i.ItemOrder).ToList();
    }
like image 362
Grasshopper Avatar asked Aug 31 '12 14:08

Grasshopper


People also ask

Is LINQ OrderBy ascending?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order.

What is difference between OrderBy and ThenBy in LINQ?

The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order.

What sort algorithm does LINQ use?

However this is not always the case, For example when using Linq on a list and calling OrderBy, Quicksort is always used as the underlying implementation.

What is OrderBy in LINQ?

LINQ includes following sorting operators. Sorting Operator. Description. OrderBy. Sorts the elements in the collection based on specified fields in ascending or decending order.


1 Answers

OrderBy creates a new query, that will, when executed, not alter your original list.

Why not just the Sort method of the List?

items.Sort((a, b) => a.ItemOrder.CompareTo(b.ItemOrder));
like image 154
sloth Avatar answered Sep 21 '22 05:09

sloth