簡體   English   中英

數組比較和排序到列表 C#

[英]Array comparison and sorting to list c#

我有包含“主題”列的數據表,數據表“ABC / Vesse / 11371503 / C 報告”中的第 1 [主題] 列數據表“值/BEY/11371503/A 報告”中的第 2 行 [主題] 列我需要比較基於 / 的主題列內的值,如果 / 之前的值相同,我應該在下一個斜杠之前查找下一個值並排序..

正如建議的那樣,我基於/ 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();
    }
}

第一行包含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));

現在更新的值包含

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

並在您的 newList 變量中根據主題再次排序,

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

這給你的最終結果變成

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

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM