score:22

Accepted answer
var name = from r in mytable
            where r.id == 0
            select r.name;

if the row is unique then you could even just do:

var row = datacontext.mytable.singleordefault(r => r.id == 0);
var name = row != null ? row.name : string.empty;

score:0

var x  =  from row in table
          where row.id == 0
          select row

supposing you have a datatable that knows about the rows, other wise you'll need to use the row index:

where row[rownumber] == 0

in this instance you'd also want to use the select to place the row data into an anonymous class or a preprepared class (if you want to pass it to another method)

score:0

use linq and set the data table as enumerable and select the fields from the data table field that matches what you are looking for.

example

i want to get the currency id and currency name from the currency table where currency is local currency, and assign the currency id and name to a text boxes on the form:

datatable dt = curdata.loadcurrency();
            var curid = from c in dt.asenumerable()
                        where c.field<bool>("localcurrency") == true
                        select c.field<int>("curid");

            foreach (int cid in curid)
            {
                txtcurid.text = cid.tostring();
            }
            var curname = from c in dt.asenumerable()
                          where c.field<bool>("localcurrency") == true
                          select c.field<string>("curname");
            foreach (string cname in curname)
            {
                txtcurrency.text = cname.tostring();
            }

score:1

var name = from datarow dr in tblclasscode.rows where (long)dr["id"] == convert.toint32(i) select (int)dr["name"]).firstordefault().tostring() 

score:2

if the return value is string and you need to search by id you can use:

string name = datatable.asenumerable().where(row => convert.toint32(row["id"]) == id).select(row => row.field<string>("name")).tostring();

or using generic variable:

var name = datatable.asenumerable().where(row => convert.toint32(row["id"]) == id).select(row => row.field<string>("name"));

score:7

i notice others have given the non-lambda syntax so just to have this complete i'll put in the lambda syntax equivalent:

non-lambda (as per james's post):

var name = from i in datacontext.mytable
           where i.id == 0
           select i.name

equivalent lambda syntax:

var name = datacontext.mytable.where(i => i.id == 0)
                              .select(i => new { name = i.name });

there's not really much practical difference, just personal opinion on which you prefer.

score:30

thanks for your answers. i didn't understand what type of object "mytable" was (in your answers) and the following code gave me the error shown below.

datatable dt = ds.tables[0];
var name = from r in dt
           where r.id == 0
           select r.name;

could not find an implementation of the query pattern for source type 'system.data.datatable'. 'where' not found

so i continued my googling and found something that does work:

var rowcoll = ds.tables[0].asenumerable();
string name = (from r in rowcoll
              where r.field<int>("id") == 0
              select r.field<string>("name")).first<string>();

what do you think?


Related Query