Linq OrderBy是否不对原始收藏进行排序? [英] Does Linq OrderBy Not Sort Original Collection?

查看:84
本文介绍了Linq OrderBy是否不对原始收藏进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用Linq To Sql作为我的数据库层,但遇到了问题.基本上,我有一个允许用户创建预定义作业的表单.预定义的作业可以具有许多预定义的作业项目.例如,预定义的工作可能类似于换油.预定义的作业项目将是石油,劳动力等.在我的数据库中,PredefinedJobs是一个表,PredefinedJobItems是另一个表,该表具有外键回到PredefinedJobs.我有一个用于添加预定义作业的表单,该表单具有Linq-to-Sql类,将该表单作为类变量支持.窗体上有一个ListView,显示所有作业项目.一个新功能要求我跟踪项目在ListView中的位置.例如,如果我的商品ListView如下所示,请注意顺序:

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

因为通过子窗体添加了项,所以我不想提供对ListView的访问.因此,我创建了以下方法,以尝试创建ItemOrder并对PredefinedJob Linq to Sql对象的集合进行排序.列表上的OrderBy函数似乎并未对PredefinedJob上的集合进行实际排序.维护Linq to Sql集合(即PredefinedJob.fkJobItems)上的顺序的最佳方法是什么?或者,最好是将对我的ListView的引用传递到子窗体中,以将这些项添加到我可以访问selectedIndex的作业中?

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();
    }

推荐答案

OrderBy创建一个新查询,该查询在执行时不会更改原始列表.

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

为什么不只是List Sort 方法?

Why not just the Sort method of the List?

items.Sort((a, b) => a.ItemOrder.CompareTo(b.ItemOrder));

这篇关于Linq OrderBy是否不对原始收藏进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆