开发者

How to join Jagged array with separator using string.Join?

How can i solve below string join error. i converted int value to string value but error occurs.... Look Join method

using Sys开发者_运维问答tem;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SortArrayYusuf
{
    class Program
    {
        static void Main(string[] args)
        {
            //int[] array;

            //using (DataTable table = GetTable())
            //{
            //    array = (from DataRow row in table.Rows
            //             select
            //             (from DataColumn col in table.Columns
            //              select row[col].ToString().Length).Sum()).ToArray();
            //}

            //foreach (int value in array)
            //    Console.WriteLine(value);

            int[][] lengths;

            using (DataTable table = GetTable())
            {
                lengths = (from DataRow row in table.Rows
                           select
                           (from DataColumn col in table.Columns
                            select row[col].ToString().Length).ToArray()).ToArray();
            }

            foreach (int[] row in lengths)
            {
                Console.WriteLine(string.Join(", ", row.ToString()));
            }
            Console.ReadKey();
        }

        static DataTable GetTable()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Patient", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));

            //
            // Here we add five DataRows.
            //
            table.Rows.Add(25, "Indocin", "David", DateTime.Now);
            table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

            return table;
        }
    }
}

 foreach (int[] row in lengths)
            {
                Console.WriteLine(string.Join(", ", row.ToString()));
            }


Console.WriteLine(string.Join(", ", row.Select(x => x.ToString()).ToArray()));


You just want all the values in turn? Then SelectMany may help. If you are on .NET 4, there is a Join overload that can help here, too (taking a sequence rather than an array, so you don't need to create an additional single array, or mess with StringBuilder):

        int[][] arr = { new[] {1,2,3}, new[] {4}, new[] {5,6}};
        string s = string.Join(",", arr.SelectMany(row => row));

which returns "1,2,3,4,5,6".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜