首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

容易的 排序 linq

2012-09-20 
简单的 排序 linq问下 DataTable 用linq怎么排序期望结果:2001年之前20012002如果按字符串升序排出来的是2

简单的 排序 linq
问下 DataTable 用linq 怎么排序

期望结果:
2001年之前
2001
2002

如果按字符串升序排出来的是
2001
2001年之前
2002

如何能牌出来希望的结果。
需要自己写排序规则么?

往高手指点如何写。

[解决办法]
看我的sequence 字段,不知道符合不?

C# code
 DataSet ds = new DataSet();                        string explorerdata = @"SELECT TMPLOGID,                                  APP,                                  FILENAME,                                  OPERATOR,                                  FILESIZE,                                  AUTHOR,                                  LOGTIME,                                  OTHER,                                  LOGSEQUENCE,                                  ISANALYSIS                                FROM TB_TMPLOG                                  WHERE 1=1 and  LogTime>='{0}' and LogTime<='{1}' and Author='{2}' and app='explorer.exe' and  decode(isanalysis,null,0,1)=0";                        explorerdata = string.Format(explorerdata, beginDate, endDate, userid);                        ds = getDataSetBySQL(explorerdata);//获取临时日志表数据                        DataTable dt = ds.Tables[0];                        //DataRow rowT = t.Rows[0];                        //object obj = rowT[6];                        //DateTime dt = Convert.ToDateTime(obj);                        //string strTime = dt.Year + "-" + dt.Month + "-" + dt.Day + " " + dt.TimeOfDay;                        /*end 获取临时日志表数据*/                        /*处理数据*/                        if (dt.Rows.Count < 1)//没数据则继续循环                            continue;                        string operators = null;                        string filenames = null;                        string sizes = null;                        string strdate = null;                        string logids = null;                        var query1 =                            from pl in dt.AsEnumerable()                            orderby pl.Field<decimal>("logsequence")                            group pl by pl.Field<string>("logtime")                                into temppl                                select new { Desc = temppl.Key, Total = temppl.Count(), detail = temppl, AA = temppl.ToArray() };
[解决办法]
排序可以用集合呀。
但我也想把代码写成可复用的。
#region T1字符串
public static void TTT1()
{
List<string> stdList = new List<string>() { "3" };
List<string> recordList = new List<string>() { "1", "0", "1", "3", "5", "1", "3", "0", "1", "3", "0", "1", "3", "1", "3", "0" };

List<string> nextList = FindNext(stdList, recordList);
foreach (var item in nextList)
{
Console.WriteLine(item.ToString());
}

}
/// <summary>
/// 字符串
/// </summary>
/// <param name="stdList"></param>
/// <param name="recordList"></param>
/// <returns></returns>
public static List<string> FindNext(List<string> stdList, List<string> recordList)
{
List<string> lstNext = new List<string>();

int La = 0;
string[] stArr = stdList.ToArray();
foreach (var item in recordList)
{
Console.Write(" : " + item.ToString());
int vResult = 0;
for (int i = 0; i < stArr.Length; i++)


{
if (La + stArr.Length < recordList.Count && recordList[La + i] == stArr[i])
{
vResult++;
}
else
{
break;
}

}
Console.Write(" " + vResult.ToString());
//
if (vResult == stArr.Length && La + vResult < recordList.Count)
{
lstNext.Add(recordList[La + vResult]);
Console.WriteLine(" " + recordList[La + vResult].ToString());
}
else
{
Console.WriteLine(" ");
}
La++;
}
return lstNext;
}

#endregion

热点排行