简体   繁体   English

数组比较和排序到列表 C#

[英]Array comparison and sorting to list c#

I have data table with column "Subject", row 1 [subject] column in data table "ABC / Vesse / 11371503 /C report " row 2 [subject] column in data table "Value/BEY/11371503/A report" I need to compare the values inside subject column based on / and if the value before / is same,i should look for next value before next slash and sort..我有包含“主题”列的数据表,数据表“ABC / Vesse / 11371503 / C 报告”中的第 1 [主题] 列数据表“值/BEY/11371503/A 报告”中的第 2 行 [主题] 列我需要比较基于 / 的主题列内的值,如果 / 之前的值相同,我应该在下一个斜杠之前查找下一个值并排序..

As suggested, I'm splitting based on / strSubSplit.正如建议的那样,我基于/ strSubSplit 进行拆分。 Can please help how to sort and add to the list after comparing.可以请帮助如何排序和比较后添加到列表中。 Thanks a lot.非常感谢。

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

First Row contains ABC / Vesse / 11371503 /C report and Second contains Value /BEY /11371503 /A report第一行包含ABC / Vesse / 11371503 /C report ,第二行包含Value /BEY /11371503 /A report

Try,尝试,

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

now updated values are contains现在更新的值包含

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

and in your newList variables sorted again based on the subject,并在您的 newList 变量中根据主题再次排序,

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

Which give your final result becomes这给你的最终结果变成

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

Try this:尝试这个:

// 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