如何使用 Linq 对通用列表进行排序?
我如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
或者
or
您可以使用 OrderBy 或 Sort,但是您应该了解两者之间的区别:
如果您进行排序,它会“就地”对列表进行排序,因此在本例中,变量“list”会被排序:
如果您这样做一个 OrderBy,原始列表不受影响,但返回一个新的、排序的枚举:
编辑
这个答案最近被投票了,所以我回顾了它。在我最初的回复中,我遗漏了一个非常重要的细节:
如果您使用上面的 LINQ 代码(第二个示例),则每次迭代变量“sorted”时都会进行排序。所以,如果你有超过 1 个 foreach,你就会重复排序。为了避免这种情况,请将上面的代码更改为:
这将强制枚举器运行,并将结果存储在排序中。
如果您只想枚举一次,则可以省略 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:
If you do an OrderBy, the original list is unaffected, but a new, sorted enumerable is returned:
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:
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.
您应该能够在列表中使用 OrderBy 方法。
You should be able to use the OrderBy method on your list.
看看这个答案。您可以通过获取列表的 typeOf 将“Process”替换为您的对象。
干杯
Take a look at this answer. You can replace "Process" with your object by getting typeOf your List.
Cheers