How to find column by name in datatable using LINQ and return ordinal position after found

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
this way i search data in datatable and get row index where data found

string strSec = "Section";
string strLi = "LineItem";

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2]{
new DataColumn("Section", typeof(string)),
new DataColumn("LineItem", typeof(string))});

dt.Rows.Add("Consensus Model", "Net Sales");
dt.Rows.Add("Consensus Model", "Cost of Sales-GAAP");
dt.Rows.Add("Consensus Model", "Revenue Growth- Reported");
dt.Rows.Add("Key financials", "Gross Profit");

int index = dt.AsEnumerable().Select(row => row.Field<string>(strSec) == "Consensus Model"
&& row.Field<string>(strLi) == "Net Sales").ToList().FindIndex(col => col);

same way how can i find column by name in data table and get index of that column if found?

now i find column this way

columnIndex = dtMain.Columns.IndexOf("QCCheck");
if (columnIndex > -1)
{
dtMain.Columns["QCCheck"].ColumnName = "QC Check";
columnIndex = -1;
}

i want to search column by name in datatable using LINQ and want to get column index or ordinal position if column found.

thanks

Continue reading...
 
Back
Top