May I ask someone to help me write this in LINQ,
DataTable dt = new DataTable();
DataColumn dcValue = new DataColumn();
dcValue.ColumnName = "value";
DataColumn dvDisplay = new DataColumn();
dvDisplay.ColumnName = "display";
DataColumn dvIsDefualt = new DataColumn();
dvIsDefualt.ColumnName = "isDefalt";
dt.Columns.Add(dcValue,int);
dt.Columns.Add(dvDisplay,string);
dt.Columns.Add(dvIsDefualt,bool);
this.tipRacuna.DataSource = ds.Tables[0];
this.tipRacuna.ValueMember = "value";
this.tipRacuna.DisplayMember = "display";
this.tipRacuna.SelectedValue = findDefault(dt);//linq to find first value with default = true;
}
private int findDefault(DataTable dtt)
{
int i= 0;
foreach (DataRow dr in dtt.Rows)
{
if (bool.Parse(dr["isDefalt"].ToString()))
{
return int.Parse(dr["value"].ToString());
}
}
return i;
}
As I am getting more involved in C# and programming at all, I am getting more intersting in LINQ queries.
In begins I always skip LINQ taking argument as I know TSQL no needs for another Query language, but when I see how many lines of code i can save using LINQ i start learning it.
Assuming that your DB columns are strongly-typed (best):
With type conversion:
With parsing:
Note that I have corrected the spelling of “isDefalt” to “isDefault”.