2015-10-25 155 views
-2

我有數據表列「數據表中的數據表」ABC/Vesse/11371503/C報告「行2 [主題]列中的列」主題「,行1 [主題]/BEY/11371503/A報告「我需要比較基於/的主題欄內的值,如果/之前的值相同,我應該在下一個斜槓和排序前查找下一個值。」陣列比較和排序列表c#

如上所述,根據/ strSubSplit分割。比較之後,請幫助如何排序並添加到列表中。非常感謝。

if (ds.Tables[0].Rows.Count > 0) 
{ 
    foreach (DataRow row in ds.Tables[0].Rows) 
    { 
     string strSubject = row["Subject"].ToString(); 
     string strEmailFrom = row["EmailFrom"].ToString(); 
     string strEmailTo = row["EmailTo"].ToString(); 
     string strEmailCC = row["EmailCc"].ToString(); 
     string strEmailContent = row["EmailContent"].ToString(); 

     // Do proper error handling here 
     DateTime strCreatedOn = DateTime.Parse(row["CreatedOn"].ToString()); 
     string[] strSubSplit= row["Subject"].ToString().Split(new[] {'/'},StringSplitOptions.RemoveEmptyEntries); 
     mailInfoList.Add(new Tuple<string, string, string, string, string, DateTime>(strSubject, strEmailFrom, strEmailTo, strEmailCC, strEmailContent, strCreatedOn)); 

     var newList = mailInfoList.OrderBy(x => x.Item1).ThenBy(x => x.Item6).ToList(); 
    } 
} 
+3

請清楚描述你的要求:) –

+0

意思是「ABC/Vesse/11371503/C re端口「是一個列值? –

+0

您的意思是「ABC/Vesse/11371503/C報告」是否按照「11371503/ABC/C report/Vesse」的排序順序排列? –

回答

0

試試這個:

// assuming you'll already have the subjects of the emails 
var subjects = new List<string> 
{ 
    "ABC/Vesse/11371503 /C report", 
    "Value/BEY/11371503/A report" 
}; 

var listOfItems = new List<Tuple<string, string, int, string>>(); 
subjects.ForEach(s => 
{ 
    var splits = s.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray(); 

    // Do proper error checking before the int.Parse and make sure every array has 4 elements 
    listOfItems.Add(
     new Tuple<string, string, int, string>(splits[0], 
               splits[1], 
               int.Parse(splits[2]), 
               splits[3])); 
}); 

var newList = listOfItems 
    .OrderBy(x => x.Item3) // the number 
    .ThenBy(x => x.Item4) // {x} report 
    .ToList(); 
+0

這將幫助我..感謝很多..將盡力 – Priya

+0

很高興我可以幫助:) – Nasreddine

+0

由於ur代碼,排序的主題被添加到newList .how can i將排序列表添加到現有列表中以獲取所有值。感謝您的所有幫助。 – Priya

0

第一行包含ABC/Vesse/11371503 /C report 和第二個包含Value /BEY /11371503 /A report

嘗試,

string strSubject = string.Join("/",row["Subject"] 
            .ToString() 
            .Split(new[] { '/' }, 
            StringSplitOptions.RemoveEmptyEntries 
           ).ToList().OrderBy(x => x)); 

現在更新的值是包含

行1 11371503 /ABC /C report/Vesse 行2 11371503 /A report/BEY /Value

並重新排序基於主題的newList變量,

var newList = mailInfoList.OrderBy(x => x.Item1).ThenBy(x => x.Item6).ToList(); 

這給你的最終結果變得

行1 11371503 /A report/BEY /Value 行2 11371503 /ABC /C report/Vesse