tags:

views:

125

answers:

4

Let's ay I have this query:

var results = from row in db.Table select row;

How can I access this:

string name = results[0]["columnName"];
+1  A: 

results is a IEnumerable list of Rows. So you can get it with a simple foreach.

foreach(var row in results)
{
   string name = row["columnName"];
}
David Basarab
+1  A: 

(from row in db.Table select row).First().columnName

Dave
+2  A: 

If you specifically just want the zeroth element, you can use results.First()

Jim Lamb
I don't think he was just looking for the zeroth element...
Will
+1  A: 

if you really want a particular index you can use the Skip() method with First().

var rowOffset = 0;
var results = (from row in db.Table 
               select row).Skip(rowOffset).First()["columnName"];

But unless you are using a Where clause I would really recommend using the indexer. The indexer is pretty much a direct reference while the LINQ statement would be using the objects iterator.

Also don't forget you can do much more advanced stuff with LINQ.

var rowOffset = 0;
var pageLength = 10;
var results = (from row in db.Table 
               let colValue = row["columnname"]
               where colValue != null
               select colValue.ToString()
               ).Skip(rowOffset)
               .Take(pageLength)
               .ToArray();
var commaString = string.Join(", ", results);
Matthew Whited