Custom Linq Ordering

前端 未结 5 1501
小蘑菇
小蘑菇 2021-02-09 00:34

I have over a thousand folders, each folder contains one or more files with the following names:

Unordered:

Alison.ext
Heather.ext
Molly.ext
Paula.ext
Sam.ext         


        
5条回答
  •  臣服心动
    2021-02-09 00:48

    You can store the desired order list in an array

    int[] iIndex = {3,2,0,4, 1};
    

    Say str holds your unordered List

    List str = new List();
    str.Add("Alison.ext");
    str.Add("Heather.ext");
    .
    .
    .
    

    Add your list and corresponding order into a datatable

    DataTable dt = new DataTable();
                    dt.Columns.Add("Order", typeof(Int32));
                    dt.Columns.Add("Name");
    
                    for (int iCount =0; iCount< str.Count ; iCount ++)
                    { 
                        DataRow drow1 = dt.NewRow();
                        drow1[0] = iIndex[iCount];
                        drow1[1] = str[iCount];
                        dt.Rows.Add(drow1);
    
                    }
                    dt.AcceptChanges();
    

    Fynally Order yuor list to get yuor expected list

     var result = from ls in dt.AsEnumerable()
                             orderby ls.Field("Order")
                             select ls;      
    

提交回复
热议问题