如何使用 Linq 对通用列表进行排序?

发布于 2024-09-06 12:34:41 字数 1388 浏览 2 评论 0原文

我如何在 linq 中对 myScriptCellsCount.MyCellsCharactersCount (列表 int 类型)进行排序

    public class MyExcelSheetsCells
    {
        public List<int> MyCellsCharactersCount { get; set; }

        public MyExcelSheetsCells()
        {
            MyCellsCharactersCount = new List<int>();
        }

    }
   void ArrangedDataList(DataTable dTable)
        {
            DAL.MyExcelSheets myexcelSheet = new DAL.MyExcelSheets();
            myScriptCellsCount = new TestExceltoSql.DAL.MyExcelSheetsCells();

            foreach (DataColumn col in dTable.Columns)
                myexcelSheet.MyColumnNames.Add(col.ColumnName.ToString());
            foreach(DataColumn dc in dTable.Columns)
            foreach (DataRow  dr in dTable.Rows)
                myScriptCellsCount.MyCellsCharactersCount.Add(dr[dc].ToString().Length);
          //How can i sort desc
            //myScriptCellsCount.MyCellsCharactersCount = from list in myScriptCellsCount.MyCellsCharactersCount
            //                                            orderby list.CompareTo( descending
            //                                            select list;
            CreatSqlTable(myexcelSheet.MyColumnNames, dTable.TableName, myScriptCellsCount.MyCellsCharactersCount[0].ToString());
            myscript.WriteScript(myscript.SqlScripts);
        }

How can i sort myScriptCellsCount.MyCellsCharactersCount (list int type) in linq

    public class MyExcelSheetsCells
    {
        public List<int> MyCellsCharactersCount { get; set; }

        public MyExcelSheetsCells()
        {
            MyCellsCharactersCount = new List<int>();
        }

    }
   void ArrangedDataList(DataTable dTable)
        {
            DAL.MyExcelSheets myexcelSheet = new DAL.MyExcelSheets();
            myScriptCellsCount = new TestExceltoSql.DAL.MyExcelSheetsCells();

            foreach (DataColumn col in dTable.Columns)
                myexcelSheet.MyColumnNames.Add(col.ColumnName.ToString());
            foreach(DataColumn dc in dTable.Columns)
            foreach (DataRow  dr in dTable.Rows)
                myScriptCellsCount.MyCellsCharactersCount.Add(dr[dc].ToString().Length);
          //How can i sort desc
            //myScriptCellsCount.MyCellsCharactersCount = from list in myScriptCellsCount.MyCellsCharactersCount
            //                                            orderby list.CompareTo( descending
            //                                            select list;
            CreatSqlTable(myexcelSheet.MyColumnNames, dTable.TableName, myScriptCellsCount.MyCellsCharactersCount[0].ToString());
            myscript.WriteScript(myscript.SqlScripts);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

许一世地老天荒 2024-09-13 12:34:41
// using Linq
MyCellsCharactersCount.OrderBy(x => x);            // ascending
MyCellsCharactersCount.OrderByDescending(x => x);  // descending

或者

// not using Linq
MyCellsCharactersCount.Sort();                     // ascending
MyCellsCharactersCount.Sort().Reverse();           // descending
// using Linq
MyCellsCharactersCount.OrderBy(x => x);            // ascending
MyCellsCharactersCount.OrderByDescending(x => x);  // descending

or

// not using Linq
MyCellsCharactersCount.Sort();                     // ascending
MyCellsCharactersCount.Sort().Reverse();           // descending
娇俏 2024-09-13 12:34:41

您可以使用 OrderBy 或 Sort,但是您应该了解两者之间的区别:

如果您进行排序,它会“就地”对列表进行排序,因此在本例中,变量“list”会被排序:


// you can manipulate whether you return 1 or -1 to do ascending/descending sorts
list.Sort((x, y) =>
{
   if (x > y) return 1;
   else if (x == y) return 0;
   else return -1;
});

如果您这样做一个 OrderBy,原始列表不受影响,但返回一个新的、排序的枚举:

var sorted = list.OrderByDescending(x => x)

编辑

这个答案最近被投票了,所以我回顾了它。在我最初的回复中,我遗漏了一个非常重要的细节:

如果您使用上面的 LINQ 代码(第二个示例),则每次迭代变量“sorted”时都会进行排序。所以,如果你有超过 1 个 foreach,你就会重复排序。为了避免这种情况,请将上面的代码更改为:

var sorted = list.OrderByDescending(x => x).ToList(); // or .ToArray()

这将强制枚举器运行,并将结果存储在排序中。

如果您只想枚举一次,则可以省略 ToList/ToArray 调用。

You can use OrderBy or Sort, but there is a difference between the 2 that you should understand:

If you do sort, it sorts your list "in place", so in this example, the variable "list" gets sorted:


// you can manipulate whether you return 1 or -1 to do ascending/descending sorts
list.Sort((x, y) =>
{
   if (x > y) return 1;
   else if (x == y) return 0;
   else return -1;
});

If you do an OrderBy, the original list is unaffected, but a new, sorted enumerable is returned:

var sorted = list.OrderByDescending(x => x)

Edit

This answer was recently upvoted, so I reviewed it. In my original response, I left out a really important detail:

If you use the LINQ code above (second example), the sort is going to occur every time you iterate over the variable "sorted". So, if you have it in more than 1 foreach, you will repeat the sort. To avoid that, change the code above to:

var sorted = list.OrderByDescending(x => x).ToList(); // or .ToArray()

That will force the enumerator to run, and will store the result in sorted.

If you are only going to enumerate it once, you can leave out the ToList/ToArray call.

落花随流水 2024-09-13 12:34:41

您应该能够在列表中使用 OrderBy 方法。

IEnumerable sortedList = myScriptCellsCount.MyCellsCharactersCount.OrderBy(anInt => anInt);

You should be able to use the OrderBy method on your list.

IEnumerable sortedList = myScriptCellsCount.MyCellsCharactersCount.OrderBy(anInt => anInt);
千鲤 2024-09-13 12:34:41
var sorted = from i in MyCellsCharacterCount orderby i descending select i;
var sorted = from i in MyCellsCharacterCount orderby i descending select i;
2024-09-13 12:34:41

看看这个答案。您可以通过获取列表的 typeOf 将“Process”替换为您的对象。

干杯

Take a look at this answer. You can replace "Process" with your object by getting typeOf your List.

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文