开发者

How can i fill my array from whole cells of datatable?

i have a datatable i created below i need to list all rows' cell length in datatable. my result must be not including "0" value. But my list : 19,19,19,19,19, 0.0.0.0..0.0..... so on why is it? How can i see length of my Array?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataTables
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable table = GetTable();
            int[] mySortedLists = new int[table.Rows.Count*table.Columns.Count];
            foreach (DataColumn dc in table.Columns)
            {
                foreach (DataRow dr in table.Rows)
                {
                    Console.WriteLine(dr[dc].ToString().Length);
                }
                Console.WriteLine("\t");
            }

            Console.WriteLine("--------------------------------------");

            for (int i = 0; i < table.Rows.Count; i++)
           {
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    mySortedLists[i] += table.Rows[i][j].ToString().Length;
                }
            }

            foreach (var mySortedList in mySortedLists)
            {
                Console.WriteLine(mySortedList.ToString() + "\n");
            }

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

Please help me!


You declare mySortedLists to have length table.Rows.Count * table.Columns.Count, but you only use the first table.Rows.Count entries. If you want one length value per row then you probably want:

int[] mySortedLists = new int[table.Rows.Count];

Or, if you want one length value per cell then you either want a two-dimensional array:

int[,] mySortedLists = new int[table.Rows.Count, table.Columns.Count];
...
mySortedLists[i, j] += table.Rows[i][j].ToString().Length;

Or you want to flatten the array indices:

mySortedLists[i * table.Columns.Count + j] += table.Rows[i][j].ToString().Length;


Unless I'm misunderstanding what you're trying to accomplish, your problem is in how you're declaring the mySortedLists array. You want it to declare it like this:

int[] mySortedLists = new int[table.Rows.Count];

To see the length of your array, you can use

Console.WriteLine(mySortedLists.Length);


I can't tell if you're trying to get the total length of the cells of each row, or the individual lengths of each cell in each row. Your array is declared with a length that would support the latter scenario, but you're only assigning values to it for the former scenario. Here are two Linq methods to get the length values stored, the first being putting the lengths into a jagged array that will have each field length.

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

The second starts the same, but performs an aggregation at the end where the first .ToArray() was before, so it gets the total length of each individual row and stores it in an array.

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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜