How to convert nested for loops with if condition to LINQ - c#

I have a horrible method that extracts data from a DataTable and converts it to a desirable formatted DataTable. I'm sure there is a much nicer way to do this in LinQ but I'm not really experienced with it. I would appreciate if somebody could show me a nicer solution.
Heres the code
private static void ExtractImportLayoutFromExcelDt(DataTable importDt, DataTable dtExtracted, int languages)
{
// The number of Locale colums included in the excel file.
for (int x = 0; x < languages; x++)
{
// The total number of friendlynames-keys / language included in the excel.
for (int j = 0; j < dtExtracted.Rows.Count; j++)
{
var row = dtExtracted.Rows[j];
DataRow tempRow = importDt.NewRow();
// Filling in the 3 columns. (FriendlyName - LocaleID - Text)
for (int i = 0; i <= 2; i++)
{
if (i == 0)
{
tempRow[i] = row[i]; // Friendly names: This is always going to be column 1 [0].
}
else if (i == 1)
{
tempRow[i] = Regex.Match(dtExtracted.Columns[x + 1].ToString(), #"\d+").Value; // LocaleIDs: Getting rid of non numeric characters from this column.
}
else
{
tempRow[i] = row[x + 1];
}
}
importDt.Rows.Add(tempRow);
}
}
}

i would rewrite inner for loops
for (int x = 0; x < languages; x++)
foreach (DataRow row in dtExtracted.Rows)
importDt.Rows.Add
(
row[0],
Regex.Match(dtExtracted.Columns[x + 1].ToString(), #"\d+").Value; // LocaleIDs: Getting rid of non numeric characters from this column.
row[x + 1]
);
DataTable.Rows collection has overload of Add method, which accept an array of objects: Add

I am not quite sure if LINQ could be of any help here, but apart from rewriting the entire mapping logic I would at least split this method into two:
private static void ExtractImportLayoutFromExcelDt(DataTable importDt, DataTable dtExtracted, int languages)
{
// The number of Locale colums included in the excel file.
for (int x = 0; x < languages; x++)
{
// The total number of friendlynames-keys / language included in the excel.
for (int j = 0; j < dtExtracted.Rows.Count; j++)
{
AddRow(importDt, dtExtracted, dtExtracted.Rows[j], x+1);
}
}
}
private static void AddRow(DataTable table, DataTable dtExtracted, DataRow originalRow, int language)
{
var row = table.NewRow();
row[0] = originalRow[0];
row[1] = Regex.Match(dtExtracted.Columns[language].ToString(), #"\d+").Value;
row[2] = originalRow[language];
table.Rows.Add(row);
}

You can write something like this but in this case is more like abuse of Linq but if you do it for educational purposes ..The main power of LINQ is when you want to enumerate or filter for example collections not in cases like this.
public static void ExtractImportLayoutFromExcelDt(DataTable importDt, DataTable dtExtracted, int languages)
{
Enumerable.Range(0, languages)
.ToList().ForEach(x =>
{
Enumerable.Range(0, dtExtracted.Rows.Count)
.ToList().ForEach(j =>
{
var row = dtExtracted.Rows[j];
DataRow tempRow = importDt.NewRow();
AddRow(importDt, dtExtracted, x, row, tempRow);
});
});
}
private static void AddRow(DataTable importDt, DataTable dtExtracted, int x, DataRow row, DataRow tempRow)
{
for (int i = 0; i <= 2; i++)
{
if (i == 0)
{
tempRow[i] = row[i]; // Friendly names: This is always going to be column 1 [0].
}
else if (i == 1)
{
tempRow[i] = Regex.Match(dtExtracted.Columns[x + 1].ToString(), #"\d+").Value; // LocaleIDs: Getting rid of non numeric characters from this column.
}
else
{
tempRow[i] = row[x + 1];
}
}
importDt.Rows.Add(tempRow);
}

Related

I Want to verify that Sum of 3 Row values is equal to the first row in on my WebTable using Selenium C#. The Rows are Row3, Row6 & Row8

IList<IWebElement> rows = _driver.FindElements(By.XPath("//div[#id='data_3']/div/div/div[3]/table/tbody/tr[#class]"));
for (int i = 1; i <= rows.Count; i++)
{
IList<IWebElement> columns = _driver.FindElements(By.XPath("//div[#id='data_3']/div/div/div[3]/table/tbody/tr[#class][" + i + "]/td[#class]"));
for (int j = 1; j <= columns.Count; j++)
{
}
}
From what I understand, you need to sum values from three rows and check if it is equal to the value from the first one. I will use the path you have provided.
public int GetRowValue(int rowNumber)
{
var row = _driver.FindElements(By.XPath("//div[#id='data_3']/div/div/div[3]/table/tbody/tr[#class]"))[rowNumber - 1] // decreased by 1, because if you would like to get first row, it would have 0th index in the array
var columns = row.FindElements(By.XPath("./td"); // find all tds of the given row.
int value = 0;
foreach(var column in columns)
{
if (String.IsNullOrEmpty(column.Text)) continue;
result += Int32.Parse(column.Text);
}
return value;
}
With that, you can easily check what you want.
var row1Value = GetRowValue(1);
var sum = GetRowValue(3);
sum += GetRowValue(6);
sum += GetRowValue(8)
Assert.Equals(row1Value, sum);

Excel Interop Efficiency get row number with value in a column

Currently I am using the below segement of code to get a row with a value int in the 3rd column.
private int getRowByRDS(int id)
{
int bestfit = -1;
Boolean foundOne = false;
for (int i = 2; i < oSheet.Rows.Count; i++)
{
string val = getValueOfCell(i, 3);
if (val == null)
continue;
int rds = int.Parse(val);
if (rds == id)
{
bestfit = i;
foundOne = true;
}
else
if (foundOne)
return bestfit;
}
return bestfit;
}
The issues is that this is pathetically show at large amount of rows.
Can someone suggest a better way of searching col 3 for a int and returning the last row # that it is in.
For Joe:
public void inputRowData(string[] data, int rds)
{
int bestRow = getRowByRDS_a(rds);
string[] formatedData = formatOutput(bestRow, data);
string val = getValueOfCell(bestRow, 6);
if (val != null)
{
shiftRows(bestRow, data.Length);
bestRow++;
}
else
shiftRows(bestRow, data.Length - 1);
// transform formated data into string[,]
string[][] splitedData = formatedData.Select(s => s.Split('\t')).ToArray();
var colCount = splitedData.Max(r => r.Length);
var excelData = new string[splitedData.Length, colCount];
for (int i = 0; i < splitedData.Length; i++)
{
for (int j = 0; j < splitedData[i].Length; j++)
{
excelData[i, j] = splitedData[i][j];
}
}
oSheet.get_Range("A" + bestRow.ToString()).Resize[splitedData.Length, colCount].Value = excelData;
MainWindow.mainWindowDispacter.BeginInvoke(new System.Action(() => MainWindow.mainWindow.debugTextBox.AppendText("Done with " + rds + " input!" + Environment.NewLine)));
}
private void shiftRows(int from, int numberof)
{
from++;
Range r = oXL.get_Range("A" + from.ToString(), "A" + from.ToString()).EntireRow;
for (int i = 0; i < numberof; i++)
r.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown);
}
Here's how I'd do it:
Get a Range corresponding to the column you're interested in
Get the UsedRange of the sheet you're interested in
Get a Range that is the intersection of the above two ranges
Get the value of this Range, which will be an array of values from the column you're interested in
You can then iterate through this array to find the value you want, then use its index to derive the row number.
The number of calls to the Excel is O(1) with the above method, as opposed to O(n) in your version.
You could also use the Find method of the Excel.Range object. This method returns a Excel.Range object if a match is found or null. Follow the first three steps that Joe described to create the search range. If the Find method returns a valid Range, you can then use it's Row Property.
P.S.: Sorry Joe I wanted to upvote your answer, but I am not allowed to do so yet.

Excel Interop - insert & add data by row

Hey guys I need to insert empty rows below a certain row in an excel and then add data into those empty rows I inserted...
So far I am able to create empty rows but I am having a hell of a time trying to figure out how to set Range.Value to an array of type String
Method for inserting Rows:
private void shiftRows(int from, int numberof)
{
from++;
Range r = oXL.get_Range("A" + from.ToString(), "A" + from.ToString()).EntireRow;
for (int i = 0; i < numberof; i++)
r.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown);
}
// so this would shift the below rows by numberof times.
This method is currently what I am stuck on... which is inserting an array into the new rows one row at a time
public void inputRowData(string[] data, int rds)
{
int bestRow = getRowByRDS(rds);
string val = getValueOfCell(bestRow, 6);
if (val == null || val.Equals(""))
{
shiftRows(bestRow, data.Length);
string[] formatedData = formatOutput(bestRow, data);
for (int i = 0; i < formatedData.Length; i++)
{
Range r = oSheet.get_Range((bestRow + i).ToString() + ":" + (bestRow + i).ToString());
r.set_Value(formatedData[i].Split('\t'));
// have tried r.Value = formatedData[i].Split('\t')
// formatedData is an array of string which contains data for each cell seperated by a tab
}
}
else
{
Console.WriteLine("Line has some information already, skipping 1 more");
shiftRows(bestRow, data.Length + 1);
}
}
I strongly advise you:
NOT to insert rows but just write empty row instead (safety and performance)
to set a big array object and do only ONE write in excel (performance)
example (i kept the shiftrows but you should really get rid of it):
public void inputRowData(string[] data, int rds)
{
int bestRow = getRowByRDS(rds);
string val = getValueOfCell(bestRow, 6);
if (val == null || val.Equals(""))
{
shiftRows(bestRow, data.Length);
string[] formatedData = formatOutput(bestRow, data);
// transform formated data into string[,]
var string[][] splitedData = formatedData.Select(s => s.Split('\t')).ToArray();
var colCount = splitedData.Max(r => r.Lenght);
var excelData = new string[splitedData.Length, colCount]
for (int i = 0; i < splitedData.Length; i++)
{
for (int j = 0; j < splitedData[i].Length; j++)
{
excelData[i,j] = splitedData[i][j];
}
}
oSheet.get_Range("A" + bestRow.ToString()).Resize(splitedData.Length, colCount).Value = excelData;
}
else
{
Console.WriteLine("Line has some information already, skipping 1 more");
shiftRows(bestRow, data.Length + 1);
}
}

Sum of rows in DataTable -- invalid cast?

1.) ***Ok, so I have my DataTable that is imported from an Excel SpreadSheet and it is filled.I would like to sift through the DataTable and sum each row. I have to skip the first column & the first rows because they are labels. I am trying to reach each row in the table and total it and output to a "Row Total" column .. I am getting a "invalid cast specified" when I am trying to assign the 'number' variable to try and sum each cells value.
Example:
Row(0) --------------------ItemA......ItemB.......ItemC..............RowTotal
Column(1) CompanyA .....12 ..........12.............10....................34
2.) Also, I haven't reached it yet -- there is a potential issue with my trying to output it to the last column in the DataTable.
noted by: dr[dt.Columns.Count - 1] = Convert.ToInt32(sum);
Any thoughts or suggestions?
DataRow dr = dt.NewRow();
int sum = 0;
dt.Columns.Add("Row Totals", typeof(int));
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].ColumnName == "Client")
{
dt.Columns.Cast<DataColumn>().Skip(1);
}
else
{
for (int j = 0; j < dt.Rows.Count - 1; j++)
{
dt.Rows.Cast<DataRow>().Skip(1);
int number =0;
number = (dt.Rows[j].Field<int>(i));
sum += number;
dr[dt.Columns.Count - 1] = Convert.ToInt32(sum);
Console.WriteLine("Row : {0} , Column : {1} , Value : {2}", i,j, dt.Rows[i][j].ToString());
Console.WriteLine(sum);
}
Console.ReadLine();
}
}
UPDATE: 12/27/12 ************
So, a solution I'm trying is to just skip the rows and column I know are text. I am still getting the "specified cast not valid" when it tries to filter through each cell and sum it. Any more suggestions?
Thank you in advance.
DataRow dr = dt.NewRow();
int sum = 0;
int number = 0;
for (int i = 0; i < dt.Columns.Count-1; i++)
{
if (dt.Columns[i].ColumnName == "column1")
{
dt.Columns.Cast<DataColumn>().Skip(0);
}
if (dt.Columns[i].ColumnName == "column2")
{
dt.Columns.Cast<DataColumn>().Skip(1);
}
else
{
for (int j = 0; j < dt.Rows.Count; j++)
{
dt.Rows.Cast<DataRow>().Skip(0);
dt.Rows.Cast<DataRow>().Skip(1);
//if (number != -1 && number != 0)
//{
number = (dt.Rows[j].Field<int>(i));
sum += number;
dr[dt.Rows.Count] = Convert.ToInt32(sum);
//}
//else
//{
// number = 0;
//}
Console.WriteLine("Row : {0} , Column : {1} , Value : {2}", i, j, dt.Rows[i][j].ToString());
Console.WriteLine(sum);
}
Console.ReadLine();
dataGridView1.DataSource = dt;
}
**UPDATE 12/27 1:30pm
I have scratched all that previous code and am attempting a test sheet and output. It seems to be working except now I can't seem to get the items to total when I am adding them to the last row. I'm stuck in the "else"" section of the code.
for (int i = 0; i < dt.Columns.Count - 2; i++)
{
for (int j = 0; j < dt.Rows.Count - 1; j++)
{
string value = dt.Rows[i][j].ToString();
int num = 0;
bool res = int.TryParse(value, out num);
if (res == false)
{
num = 0;
}
else
{
int sum = 0;
sum += num;
DataRow dr;
dr["Totals"] = sum;
dt.Rows.Add(dr);
}
}
dataGridView1.DataSource = dt;
}
}
One possibility is that there is a blank value in your data table - you can't cast a null object to an integer, so the Field extension method may be failing.

Reading data from DataGridView in C#

How can I read data from DataGridView in C#? I want to read the data appear in Table. How do I navigate through lines?
something like
for (int rows = 0; rows < dataGrid.Rows.Count; rows++)
{
for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++)
{
string value = dataGrid.Rows[rows].Cells[col].Value.ToString();
}
}
example without using index
foreach (DataGridViewRow row in dataGrid.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
string value = cell.Value.ToString();
}
}
If you wish, you can also use the column names instead of column numbers.
For example, if you want to read data from DataGridView on the 4. row and the "Name" column.
It provides me a better understanding for which variable I am dealing with.
dataGridView.Rows[4].Cells["Name"].Value.ToString();
Hope it helps.
string[,] myGridData = new string[dataGridView1.Rows.Count,3];
int i = 0;
foreach(DataRow row in dataGridView1.Rows)
{
myGridData[i][0] = row.Cells[0].Value.ToString();
myGridData[i][1] = row.Cells[1].Value.ToString();
myGridData[i][2] = row.Cells[2].Value.ToString();
i++;
}
Hope this helps....
Code Example : Reading data from DataGridView and storing it in an array
int[,] n = new int[3, 19];
for (int i = 0; i < (StartDataView.Rows.Count - 1); i++)
{
for (int j = 0; j < StartDataView.Columns.Count; j++)
{
if(this.StartDataView.Rows[i].Cells[j].Value.ToString() != string.Empty)
{
try
{
n[i, j] = int.Parse(this.StartDataView.Rows[i].Cells[j].Value.ToString());
}
catch (Exception Ee)
{ //get exception of "null"
MessageBox.Show(Ee.ToString());
}
}
}
}
private void HighLightGridRows()
{
Debugger.Launch();
for (int i = 0; i < dtgvAppSettings.Rows.Count; i++)
{
String key = dtgvAppSettings.Rows[i].Cells["Key"].Value.ToString();
if (key.ToLower().Contains("applicationpath") == true)
{
dtgvAppSettings.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
}
}

Categories

Resources