Reputation: 2764
I have these 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
like this:
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 array's index must be int
type. I have no idea how to work this out:
ms[(string)dr.ItemArray[0]] = (string)dr.ItemArray[1];
Update:
Code format is a string like this: [001-001-001]
, so I can't convert it to integer.
Upvotes: 6
Views: 3532
Reputation: 7797
If you just want to get a list of all the messages from the database, then 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 couple million records. List
will perform fine in case you want to access items sequestially. If you want to access items in a non-sequential manner, I suggest you use Dictionary
instead (to improve search perfromance):
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 message as follows:
var message = ms["001-001-001"];
It will be orders or magnitude faster than accessing a random List
item:
var message - ms.First(x => x.Code == "001-001-001");
Upvotes: 3
Reputation: 2159
you don't need Message class any more. Using a dictionary as follows 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];
}
}
}
I hope this would be helpful.
Upvotes: 5
Reputation: 94
I think what you want to perform is 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]
};
}
}
}
Upvotes: 1
Reputation: 22559
I think you just want build an array of messages from ds.Tables[0]
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 better performance make use of Parallel.ForEach -
Parallel.ForEach(ds.Tables[0].AsEnumerable(), row => {
ms[i].Code= (string)row.ItemArray[0];
ms[i].Message = (string)row.ItemArray[1];
});
Upvotes: 3