数组比较和排序以列出C# [英] Array comparison and sorting to list c#

查看:80
本文介绍了数组比较和排序以列出C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据表带有主题"列,数据表"ABC/Vesse/11371503/C报告"中的第1行[主题]列数据/值/BEY/11371503/A报告中的行2 [主题]列我需要比较基于/的主题列中的值,如果/之前的值相同,我应该在下一个斜杠和排序之前查找下一个值.

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

根据建议,我基于/ strSubSplit进行拆分.比较后可以帮助您如何将其排序并添加到列表中.非常感谢.

As suggested, I'm splitting based on / 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();
    }
}

推荐答案

尝试一下:

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

这篇关于数组比较和排序以列出C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆