数组比较和列表排序c#

本文关键字:排序 列表 比较 数组 | 更新日期: 2023-09-27 18:19:06

我有列"主题"的数据表,在数据表"ABC/Vesse/11371503/C报告"中第1行[主题]列在数据表"Value/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();
    }
}

数组比较和列表排序c#

试试这个:

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

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

,

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

现在更新的值包含

第一行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第二行11371503 /ABC /C report/Vesse