How can i sort Generic list with Linq?

喜夏-厌秋 提交于 2019-11-28 08:29:28

问题


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

回答1:


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

or

// not using Linq
MyCellsCharactersCount.Sort();                     // ascending
MyCellsCharactersCount.Sort().Reverse();           // descending



回答2:


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.




回答3:


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

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



回答4:


var sorted = from i in MyCellsCharacterCount orderby i descending select i;



回答5:


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

Cheers



来源:https://stackoverflow.com/questions/3062373/how-can-i-sort-generic-list-with-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!