按文件中的列对 DataTable 进行排序

Sorting a DataTable by columns in a file

我正在读取一个 csv 文件并对其进行排序。

我正在尝试对 DataTable 进行排序,以便根据数据表中的 Bankname 给我一个输出

这是我已有的代码:

            DataTable dt = new DataTable();
            dt.Columns.Add("accountholder", typeof(string));
            dt.Columns.Add("accountnumber", typeof(int));
            dt.Columns.Add("accounttype", typeof(string));
            dt.Columns.Add("bankname", typeof(string));
            dt.Columns.Add("branch", typeof(string));
            dt.Columns.Add("amount", typeof(double));
            dt.Columns.Add("date", typeof(DateTime));

            string line;

        //CultureInfo culture = CultureInfo.InvariantCulture;
        StreamReader sr = new StreamReader(@"C:\Test\debitorders.csv");
        StreamWriter sw = new StreamWriter(@"C:\Test\output.txt");

        while ((line = sr.ReadLine()) != null)
        {
            if (line.Length > 0)
            {
                string[] inputArray = line.Split(new char[] { ',' });
                dt.Rows.Add(new object[] {
                    inputArray[0].Trim().Substring(0,1),
                    inputArray[1].Trim(),
                    long.Parse(inputArray[2].Trim()),
                    inputArray[3].Trim(),
                    inputArray[4].Trim(),
                    inputArray[5].Trim(),
                    (long)(100 * double.Parse(inputArray[6].Trim())),
                    DateTime.Parse(inputArray[7].Trim())
                });



                DateTime date = account.Field<DateTime>("date");
                string[] outputLine = new string[]{
                    initial,
                    accountholder,
                    accountnumber.ToString(),
                    accounttype,
                    bankname,
                    branch,
                    amount.ToString(),
                    date.ToShortDateString()
                };
                Console.WriteLine(string.Join(",",outputLine));
            }
        }
        Console.ReadLine();

任何帮助将不胜感激。

谢谢

您的代码和发布的期望结果似乎不一致,因此您可能需要根据您的需要调整我的代码。但我的代码示例将向您展示如何摆脱 DataTable 和 DataRow 的东西。相反,尽可能使用 LINQ 方法和一个简单的辅助方法来读取 csv 文件。如果您对代码还有任何疑问,请告诉我。

//reading the csv and create anonymous object for each line
var inputEntries = File.ReadLines(@"C:\Test\debitorders.csv")
.Select(line => 
        {
            var values = line.Split(','); 
            return new 
            {
                AccountHolder = values[0].Trim().Substring(0,1) + values[1].Trim(), 
                AccountNumber = long.Parse(values[2].Trim()),
                AccountType = values[3].Trim(),
                BankName = values[4].Trim(),
                Branch = values[5].Trim(),
                Amount = 100 * double.Parse(values[6].Trim()),
                Date = DateTime.Parse(values[7].Trim())
            };
        });

var banks = inputEntries
        .OrderBy(e => e.BankName)
        .GroupBy(e => e.BankName, e => e);

//output data
foreach(var bank in banks)
{
    //output bank header
    var AccountName = bank.Key;
    if (AccountName.Length >= 16)
    {
        AccountName = AccountName.Substring(0, 16);
    }
    else
    {
        AccountName += new string(' ', 16 - AccountName.Length);
    }
    var NumberOfAccounts = bank.Count();
    var TotalAmount = bank.Select(acc => acc.Amount).Sum();
    var Header = NumberOfAccounts.ToString("000") + TotalAmount.ToString("0000000000");
    Console.WriteLine(Header);

    //sort accounts
    var sortedAccounts = bank
                        .OrderBy( acc=> acc.AccountHolder)
                        .OrderByDescending(acc => acc.Amount);

    //output accounts
    foreach( var account in sortedAccounts)
    {
        var outputLine =
            account.AccountHolder +
            account.AccountNumber +
            account.AccountType +
            account.Amount + 
            account.Date.ToShortDateString();

        Console.WriteLine(outputLine);
    }
}