Reputation: 16536
I'm trying to get all records from a table and loop through it.
Pseudo Code:
database.dbDataContext db = new database.dbDataContext();
protected void Page_Load(object sender, EventArgs e)
{
List<database.User> data = db.Users.ToList();
// rows
for (int i = 0; i < data.Count; i++)
{
// columns
for (int j = 0; j < data[i].Count; j++)
{
}
}
}
I'm unsure about the syntax.
Anyone know how to do that?
Thanks in advance!
Upvotes: 2
Views: 3687
Reputation: 5501
You're close. You shouldn't need the internal loop. If your code is just this:
database.dbDataContext db = new database.dbDataContext();
protected void Page_Load(object sender, EventArgs e)
{
List<database.User> data = db.Users.ToList();
for (int i = 0; i < data.Count; i++)
{
var a = data[i].Field1;
var b = data[i].Field2;
...
}
}
It's a little cleaner to use Marc's version of the loop, but the core is basically that the items in the list are all objects with individual properties, not an array like I assume you expected by having the inner loop there.
Upvotes: 3
Reputation: 754488
Why not just like that:
database.dbDataContext db = new database.dbDataContext();
protected void Page_Load(object sender, EventArgs e)
{
foreach(database.User user in db.Users)
{
// do whatever you need to do with your `User` object here.....
// here, you have an instance of a `User` object - access its properties
// and methods like you always would on a `User` object....
}
}
Upvotes: 5