score:22
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?
Source: stackoverflow.com
Related Query
- Linq : select value in a datatable column
- Select all rows with distinct column value using LINQ
- Select distinct items by combination of two columns and where Value of third column is max- LINQ
- Get Max from column datatable or Default Value using Linq
- C# DataTable Linq select distinct values where column equals 'x'
- How to Find a DataTable Row by Column Value Using LINQ in VB.NET
- Linq to dataset select row based on max value of column
- How to select rows from a DataTable where a Column value is within a List?
- Find All Rows in DataTable Where Column Value is NOT Unique Using Linq Query
- using linq to select a column from a datatable
- Linq query to select rows where a column is a max value
- LINQ: select specific value in a datatable column
- Linq to CSV select by column value
- Partition By Logic in Code to calculate value of a DataTable Column
- replacing column values in a datatable and getting value in datatable datatype using linq
- Linq Select Substring WHERE VALUE of Column has 1
- How to use LINQ to select object with minimum or maximum property value
- Linq code to select one item
- Select All distinct values in a column using LINQ
- Select only a single column in LINQ
- Select most frequent value using LINQ
- Select single column from dataset with LINQ
- C# LINQ select from where value is not contained in array / list
- Select distinct rows from datatable in Linq
- Linq to SQL - How to find the value of the IDENTITY column after InsertOnSubmit()
- Exclude a column from a select using LINQ
- Using Linq to select maximum value in a group
- DataTable Select vs LINQ Select
- Default value for linq select item if query didn't return anything
- Linq on DataTable: select specific column into datatable, not whole table
More Query from same tag
- IQueryable changed in foreach loop not displaying in view
- Call SelectMany with Expression.Call - wrong argument
- Linq Syntax issue
- Getting unique values using Linq to Entities
- Linq: Group by multiple columns using Expression-tree syntax
- Getting the number of times an item is repeated in C#
- Linq parser issue?
- system.data.enumerablerowcollection to List<T>, in run time
- MVC linq query handle situation where sequence contains no elements
- .NET: recommended video tutorial for LINQ?
- Showing data on Bar graph using Linq and Mvc
- Get lambda expression from where clause or IQueryable/IEnumerable
- Linq Query Timing Out
- linq to entity query getting error
- Totalling inner lists by different methods on a list using Linq
- Filtering data from parameters c#
- "better" (simpler, faster, whatever) version of 'distinct by delegate'?
- select distinct out of three column connectiontable
- WPF Linq Results to datagrid
- Asynchronous EntityFramework operations
- Sum of decimal in null list causing exception
- Outer Variable Trap
- Linq to SQL to Linq compiled performance
- How to use "Group by" LINQ?
- Set property if exists in another list
- How to filter query result to list of strings
- Linq for updating property of A.property with matching B.property where A.Key == B.Key
- LINQ - convert select sum to a groupby sum
- C# LINQ Zipping 2 list of lists based on uniqueness of objects
- LINQ filtering on data only works during debug