English 中文(简体)
• 如何将通用名单与林克混为一谈?
原标题:How can i sort Generic list with Linq?

• 如何分类我的原状CellsCount.MyCellsCharacters 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);
        }
问题回答
// using Linq
MyCellsCharactersCount.OrderBy(x => x);            // ascending
MyCellsCharactersCount.OrderByDescending(x => x);  // descending

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

你可以使用命令By或Sort,但你应当理解的是:

如果你确实如此,就把你的名单“放在了”上,那么就这样,变量“清单”的分类如下:


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

如果你发出命令,原始名单不受影响,但新的、分类的电子数字将退还:

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

<><>Edit>/strong>

最近提出了这一答复,因此我审查了这一答复。 在我最初的答复中,我留下了非常重要的细节:

如果你使用上文(第二例)的LINQ代码,则每当你在变数“变数”中挥发时就会出现这种情况。 因此,如果你有1个以上的席位,你将重复这种说法。 为避免这种情况,将上述法典改为:

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

这将迫使统计员进行操作,并将储存结果。

如果你只想一列举,你就可以离开ToList/ToArray电话。

你们应该能够使用你名单上的教令方法。

IEnumerable sortedList = myScriptCellsCount.MyCellsCharactersCount.OrderBy(anInt => anInt);
var sorted = from i in MyCellsCharacterCount orderby i descending select i;

查阅,回答。 您可以将“产品”改为“产品”。 页: 1

卡车





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签