0

ジャグ配列のintの配列に格納しようとしています:

while (dr5.Read())
{                                        
   customer_id[i] = int.Parse(dr5["customer_id"].ToString());
   i++;       
}

dr5 はデータリーダーです。customer_id を配列に格納していますが、スコアも別の配列に格納したいと考えています。whileループ内に以下のようなものが欲しい

int[] customer_id = { 1, 2 };
int[] score = { 3, 4};
int[][] final_array = { customer_id, score };

誰でも私を助けてくれますか?編集:これは私が試したものです。値が表示されていません。

 customer_id =  new int[count];
 score = new int[count];
 int i = 0;
while (dr5.Read())
{ 
   customer_id[i] = int.Parse(dr5["customer_id"].ToString());
   score[i] = 32;
   i++;

}
 int[][] final = { customer_id, score };

return this.final;
4

3 に答える 3

4

より優れた、よりオブジェクト指向のアプローチは、 Scores プロパティを持つ Customer クラスを作成することです。

public class Customer
{
    public Customer()
    {
        this.Scores = new List<int>();
    }

    public IList<int> Scores { get; private set; }
}

顧客ごとに 1 つのスコアしかないことが判明したため、より正確な Customer クラスは次のようになります。

public class Customer
{
    public int Score { get; set; }
}

後で更新できるようにする必要がない場合は、Score プロパティを読み取り専用にすることを検討してください。

于 2010-05-31T08:37:19.183 に答える
1

はじめに サイズはわかりますか?もしそうなら、あなたはすることができます:

int[] customerIds = new int[size];
int[] scores = new int[size];
int index = 0;
while (dr5.Read())
{
    customerIds[index] = ...;
    scores[index] = ...;
    index++;
}
int[][] combined = { customerIds, scores };

ただし、再考することをお勧めします。本当に顧客 ID をスコアに関連付けたいと思われるので、そのためのクラスを作成します。次に、次のことができます。

List<Customer> customers = new List<Customer>();
while (dr5.Read())
{
    int customerId = ...;
    int score = ...;
    Customer customer = new Customer(customerId, score);
    customers.Add(customer);
}
于 2010-05-31T08:39:50.097 に答える
0

配列を使用する別のアイデアとして:

1 対 1 のマッピングの場合は、次のように Dictionary を一時的な保存に使用できます。

var scores = new Dictionary<int, int>();
while (dr5.Read())  
{  
   scores.Add(int.Parse(dr5["customer_id"].ToString()), int.Parse(dr5["score"].ToString()));
}  

それ以外の場合は、クラスの顧客を作成し、それからリストを作成できます。

于 2010-05-31T08:44:52.863 に答える