Array of objects with custom index

I have two classes:

public class Message { string Message; string Code; } public class MessageInitializer { DataSet ds; DataRow dr; message[] ms; } 

I want to create a constructor in MessageInitializer as follows:

 MessageInitializer() { this.ms = new Message[ds.Tables[0].Rows.Count]; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms[(string)dr.ItemArray[0]] = (string)dr.ItemArray[1]; } } 

But the array index must be int . I do not know how to do that:

 ms[(string)dr.ItemArray[0]] = (string)dr.ItemArray[1]; 

Update

The code format is a string like this: [001-001-001] , so I cannot convert it to integer.

+5
source share
4 answers

If you just want to get a list of all messages from the database, you can use the code below. Note List instead of Array . Easier to use and faster.

 public class Message { string Message; string Code; } public class MessageInitializer { DataSet ds; DataRow dr; List<Message> ms; MessageInitializer() { this.ms = new List<Message>(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms.Add(new Message { Code = dr.ItemArray[0].ToString(), Message = dr.ItemArray[1].ToString(), }); } } } 

You mentioned that you have a couple million records. List will execute just fine if you want to access items in a sequence. If you want to access elements in a non-sequential manner, I suggest using Dictionary instead (to improve your search):

 public class MessageInitializer { DataSet ds; DataRow dr; Dictionary<string, Message> ms; MessageInitializer() { this.ms = new Dictionary<string, Message>(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms.Add(dr.ItemArray[0].ToString(), new Message { Code = dr.ItemArray[0].ToString(), Message = dr.ItemArray[1].ToString(), }); } } } 

You can access the message as follows:

 var message = ms["001-001-001"]; 

This will be an order or magnitude faster than accessing a random List element:

 var message - ms.First(x => x.Code == "001-001-001"); 
+3
source

you no longer need a message class. Using the dictionary in the following way solves the problem:

 public class MessageInitializer { DataSet ds; DataRow dr; Dictionary<string, string> ms; public MessageInitializer() { this.ms = new Dictionary<string,string>(ds.Tables[0].Rows.Count); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms[(string)dr.ItemArray[0]] = (string)dr.ItemArray[1]; } } } 

Hope this would be helpful.

+5
source

I think you just need to create an array of messages from ds.Tables[0]

The code -
ms[i].Code = (string)dr.ItemArray[0];

Message -
ms[i].Message = (string)dr.ItemArray[1];

 MessageInitializer() { this.ms = new Message[ds.Tables[0].Rows.Count]; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms[i].Code= (string)dr.ItemArray[0]; ms[i].Message = (string)dr.ItemArray[1]; } } 

For best performance use Parallel.ForEach -

 Parallel.ForEach(ds.Tables[0].AsEnumerable(), row => { ms[i].Code= (string)row.ItemArray[0]; ms[i].Message = (string)row.ItemArray[1]; }); 
+3
source

I think you want to do the following:

 public class Message { public string message { get; set; } public string code { get; set; } } public class MessageInitializer { DataSet ds; DataRow dr; Message[] ms; MessageInitializer() { this.ms = new Message[ds.Tables[0].Rows.Count]; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms[i] = new Message { code = (string)dr.ItemArray[0], message = (string)dr.ItemArray[1] }; } } } 
+1
source

All Articles