How to query data table columns collection by LINQ

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

Sudip_inn

Guest
my data table has many fields like

1Q 2010 2Q 2010.....2010 FYE,

1Q 2010_Comment,2Q 2010_Comment.....2010 FYE_Comment

Now i need to know the column ordinal position of last period column. column name with _Comment start just after period columns. so if i need to know last period column ordinal position then i have to search column name end with _Comment and if period column name end A or E or FY then i need to store previous column ordinal position.

i have done the job this way without linq

private string GetLastColumns(DataTable dt)
{
string period = "";
for (int r = 0; r <= dt.Columns.Count - 1; r++)
{
if (dt.Columns[r].ColumnName.Contains("_Comment")
&& (dt.Columns[r - 1].ColumnName.EndsWith("FY") || dt.Columns[r - 1].ColumnName.EndsWith("A") || dt.Columns[r - 1].ColumnName.EndsWith("E")))
{
period = dt.Columns[r - 1].ColumnName;
break;
}
}
return period;
}


above code is working fine but i want to do the same thing with LINQ. please tell me how to do it with LINQ. thanks

Continue reading...
 
Back
Top