DataTable dt = new DataTable();
List<Control> control = new List<Control>();
foreach (DataColumn dc in dt.Columns) {
foreach (Control c in control) {
if (dc.ColumnName == c.ID) {
//Do something here
}
}
}
It's still going to do the two time scan, but if you need to check each item in one list against each item in another list, you need to do that somehow.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Marked as answer by HeartattacK on Apr 23, 2011 09:02 AM
Depends what version of .net you are using. If its 3.0 or above then you will have LINQ so you could probably write it in that syntax.
I dont think that really there would be any benefit as you could be making the code harder to read and it wouldnt reduce the number of loops you had to perform because you need to check every control in the list against every column in the table.
Marked as answer by HeartattacK on Apr 23, 2011 09:02 AM
rickycrc
Member
42 Points
120 Posts
Question about looping
Apr 20, 2011 09:14 AM|LINK
I have a control list and a datatable
DataTable dt = new DataTable(); List<Control> control = new List<Control>(); foreach (DataColumn dc in dt.Columns) { foreach (Control c in control) { if (dc.ColumnName == c.ID) { //Do something here } } }Can I write the code without 2 foreach statement?
HeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: Question about looping
Apr 20, 2011 09:31 AM|LINK
You could do:
if( dt.Columns.Any(x=> control.Any(y=> x.ColumnName == y.ID)) )
{
//do something here
}
It's still going to do the two time scan, but if you need to check each item in one list against each item in another list, you need to do that somehow.
blog: www.heartysoft.com
twitter: @ashic
rtpHarry
All-Star
56620 Points
8958 Posts
Re: Question about looping
Apr 20, 2011 09:34 AM|LINK
Depends what version of .net you are using. If its 3.0 or above then you will have LINQ so you could probably write it in that syntax.
I dont think that really there would be any benefit as you could be making the code harder to read and it wouldnt reduce the number of loops you had to perform because you need to check every control in the list against every column in the table.