foreach (System.Data.DataRow drcount in dscount.Tables[0].Rows)
{
int strsample5 =drcount[0].ToString().Trim();
}
dscount is a dataset name and drcount is datarow name.datarow has values of dataset.I need to pass the values of datarow to a two dimensional array.How can I pass values to two dimensional array?
Try this, But, am not sure about your data. here strsample5 is an integer array. I think Its better to use Dictionary or List.
for (int i=0;i< dscount.Tables[0].Rows.Count;i++ )
{
for (int j = 0; j < dscount.Tables[0].Columns.Count; j++)
{
strsample5[i][j] = Convert.ToInt32(dscount.Tables[0].Rows[i][j].ToString());
}
}
Related
I have a matrix and I want to return an array containing as elements the sum of each row elements of the matrix.
int [] sum;
for (var i = 0; i < m; i++)
{
for (var j = 0; j < result.Pages[i].Actual.Count; j++)
{
sum[i] += result.Pages[i].Actual[j];
}
}
This is how I tried to do it but seems it is not working. Any ideas?
Use var m = a.GetLength(0); to get number of rows, and var n = a.GetLength(1); to get number of columns.
now that looks like a different story after you edit:
Actually the first problem would be a NullreferenceException because int[]sum is not initialized!
Anyway, so it seems that you have an array of arrays. In this case you would need the Length of the Pages array to save your results. The first loop iterates over it using i and will run until result.Pages.Length. For each i you have correctly implemented a second loop where you sum up the result.
int [] sum = new int[result.Pages.Length];
for (var i = 0; i < result.Pages.Length; i++)
{
for (var j = 0; j < result.Pages[i].Actual.Length; j++)
{
sum[i] += result.Pages[i].Actual[j];
}
}
If you collections are List's then you need to use Count instead of Length
The Linq solution would look like this:
int [] sum = result.Pages.Select(x=>x.Sum()).ToArray();
EDIT:
double? means that you have a nullable data type. This is different from the normal double. Furthermore the default value will be null That means that you need to initialize the value at position i in sum before you add up values, otherwise the result will be null.
double? [] sum = new double?[result.Pages.Length];
for (var i = 0; i < result.Pages.Length; i++)
{
sum[i] = 0;
for (var j = 0; j < result.Pages[i].Actual.Length; j++)
{
sum[i] += result.Pages[i].Actual[j];
}
}
I'm new to C# and I can't find an example of this anywhere (although I have seen similar examples but the casting was ToString, whereas in my case I want arrays of integers). I have a DataTable object,
DataTable results = dbCon.ExecuteQuery("my query");
int m = results.Rows.Count;
int n = results.Columns.Count;
I want a jagged array consisting of n one-dimensional arrays (each array being a column of the DataTable (results, in this case).
int[][] jagged = new int[n][];
for (int i = 0; i < n; i++)
{
jagged[i] = new int[m];
//var coli = results.Columns.Cast<Array>().Select(column => Convert.ToInt32(column)).ToArray();
//jagged[i] = coli;
}
I've tried a few things (for example what I commented out) but I'm quite stuck now.
If anyone knows how to do this, please post something! Thanks!
Also, to be more specific, I'd really like to use a function like the one NumPy provides to Python... dataSet = np.array(dataList).astype('float'). Double for loops are not in my best interest as my DataTable is large.
I think you also need to make the array of type object. The DataTable DataRow holds things of type object, therefore that is what you need to store them as in your jagged array.
I think this will work:
DataTable table = dbCon.ExecuteQuery("my query");
int rowCount = table.Rows.Count;
int colCount = table.Columns.Count;
object[][] objs = new object[colCount][];
for (int currentColumn = 0; currentColumn < colCount; currentColumn++)
{
objs[currentColumn] = new object[rowCount];
for (int currentRow = 0; currentRow < rowCount; currentRow++)
{
objs[currentColumn][currentRow] = table.Rows[currentRow][currentColumn];
}
}
I think, you already have this. Try:
results.Rows[rowIndex][columnIndex]
To get it into jagged array, you may go for simple for loop:
for (int i = 0; i < results.Rows.Count; i++)
{
for (int j = 0; j < results.Columns.Count; j++)
{
jagged[i][j] = results.Rows[i][j];
}
}
You can get what you want just using LINQ against the DataTable. For each column in the DataTable, you need to iterate over the rows and select that column. You need to call Cast() to Select() on the columns and AsEnumerable() to be able to Select() on the rows:
int[][] jagged = results.Columns.Cast<DataColumn>()
.Select(
col => results.AsEnumerable()
.Select(row => row.Field<int>(col))
.ToArray()
).ToArray();
I'm trying to create an array with the following structure
D
C C
B B B
A A A A
Which will be N x N but split diagonally. Initially, all I know is the bottom row essentially just:
string[,] table = new string[n,];
How can I build on this structure so that when I get to the next row I can declare how many elements it has? Would it be something along the lines of:
for(int i = 0; i <= n; i++) {
table[i] = new string[--n]
}
The [,] syntax creates multidimensional arrays, not jagged arrays. For the latter, you would need to do this:
int n = 4;
string[][] table = new string[n][];
for (int i = 0; i < n; i++)
table[i] = new string[n-i];
Btw. you really don’t want to decrement n while looping in a loop with the condition i < n (or i <= n).
I have a array of two dimensions object[,] data;. I want to convert the first column of my object into a list<string> or a list<object>. here you have my code :
List<string> resultsFields = new List<string>();
object tmp = oRtx.get_ListFields(this.Instrument.ElementAt(i).Key.ToString(), RT_FieldRowView.RT_FRV_EXISTING, RT_FieldColumnView.RT_FCV_VALUE);
if (tmp != null)
{
object[,] data = (object[,])Convert.ChangeType(tmp, typeof(object[,]));
for (int j = 0; j < numItems; j++)
resultsFields.Add(data[j, 0].ToString());
}
in this code, I add each items on data[j, 0] into my list.
I would like to know if there is a faster method to convert the first column of my object ([0]) into a list
The only performance improvement i can see is creating the List<> with the specified capacity:
List<string> resultsFields = new List<string>(data.GetLength(0));
In the DataTable i have n number rows want add those rows in array?
DataTable tbl = new DataTable();
tbl.Rows
Rows is array of your records...
The code below stores all the information in a datatable in a double array of strings. The code should be plug-and-play, just make sure you swap out "dataTable1" with the name of your actual DataTable.
string[,] stringArray = new string[dataTable1.Rows.Count, dataTable1.Columns.Count];
for (int row = 0; row < dataTable1.Rows.Count; ++row)
{
for (int col = 0; col < dataTable1.Columns.Count; col++)
{
stringArray[row, col] = dataTable1.Rows[row][col].ToString();
}
}
To access the first item out of stringArray, simply use call the code below, using the indices as you would for a normal array.
stringArray[0, 0]