2010-06-19 4 views
-1

나는 아래에 만든 datatable 내가 datatable에서 모든 행의 셀 길이를 나열해야합니다. 내 결과는 "0"값을 포함해서는 안됩니다. 하지만 내 목록 : 19,19,19,19,19, 0.0.0.0..0.0 ..... 왜 그렇습니까? Array의 길이는 어떻게 볼 수 있습니까?어떻게 datatable의 전체 셀에서 배열을 채울 수 있습니까?

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

제발 도와주세요!

답변

0

, 당신은 사용할 수 있습니다

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

를 배열의 길이를 확인하려면하지만 당신은 첫 번째 table.Rows.Count 항목을 사용 : 당신은 다음과 같이 선언 할 . 당신이 셀 당 하나 개의 길이 값을 원하는 경우 다음 중 하나가 2 차원 배열하려면,

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

을 또는 :

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

을 또는 당신이 원하는 당신이 행 당 하나 개의 길이 값을 원하는 경우에 당신은 아마 원하는 배열 인덱스를 평평하게하기 :

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

없음은 내가 행의 셀 .... – Penguen

0

당신이 달성하려는 것을 오해하지 않는 한 mySortedLists 배열을 선언하는 방법에 문제가 있습니다. 당신은 길이 table.Rows.Count * table.Columns.Count을 가지고 mySortedLists를 선언

Console.WriteLine(mySortedLists.Length); 
0

각 행의 세포, 또는 각 행의 개별 셀 길이의 전체 길이를 얻으려고 노력하는 경우 내가 말할 수 없습니다. 배열은 후자의 시나리오를 지원하는 길이로 선언되지만 이전 시나리오의 경우에만 값을 할당합니다. 다음은 길이 값을 저장하는 두 가지 Linq 메소드입니다. 첫 번째는 길이를 각 필드 길이를 갖는 들쭉날쭉 한 배열에 넣는 것입니다.

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

동일한 시작하지만, 제 .ToArray()가 예전 끝의 응집을 수행하므로 배열의 각 행을 저장하여 전체 길이를 얻는다.

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

Console.WriteLine (string.Join (",", 행당 길이 값을 필요로 .... ...이 @Phsika을? – Penguen

+0

를 유효하지 않은 인수를 발생 가지고, 그 유효입니다 .NET 4. 3.5의 땅에서, 당신은'Console.WriteLine (string.Join (","row.Select (i => i.ToString()) .ToArray()));' –

관련 문제