using System.Data; //needed for the extension methods to work
...
var results =from myRow in myDataTable.Rowswhere myRow.Field<int>("RowNo") == 1select myRow; //select the thing you want, not the collection
var query = from p in dt.AsEnumerable()where p.Field<string>("code") == this.txtCat.Textselect new{name = p.Field<string>("name"),age= p.Field<int>("age")};
var results = from myRow in tblCurrentStock.AsEnumerable()where myRow.Field<string>("item_name").ToUpper().StartsWith(tbSearchItem.Text.ToUpper())select myRow;DataView view = results.AsDataView();
//Create DataTableDataTable dt= new DataTable();dt.Columns.AddRange(new DataColumn[]{new DataColumn("ID",typeof(System.Int32)),new DataColumn("Name",typeof(System.String))
});
//Fill with data
dt.Rows.Add(new Object[]{1,"Test1"});dt.Rows.Add(new Object[]{2,"Test2"});
//Now Query DataTable with linq//To work with linq it should required our source implement IEnumerable interface.//But DataTable not Implement IEnumerable interface//So we call DataTable Extension method i.e AsEnumerable() this will return EnumerableRowCollection<DataRow>
// Now Query DataTable to find Row whoes ID=1
DataRow drow = dt.AsEnumerable().Where(p=>p.Field<Int32>(0)==1).FirstOrDefault();//
SqlCommand cmd = new SqlCommand( "Select * from Employee",con);SqlDataReader dr = cmd.ExecuteReader( );DataTable dt = new DataTable( "Employee" );dt.Load( dr );var Data = dt.AsEnumerable( );var names = from emp in Data select emp.Field<String>( dt.Columns[1] );foreach( var name in names ){Console.WriteLine( name );}
// Serialize the DataTable to a json stringstring serializedTable = JsonConvert.SerializeObject(myDataTable);Jarray dataRows = Jarray.Parse(serializedTable);
// Run the LINQ queryList<JToken> results = (from row in dataRowswhere (int) row["ans_key"] == 42select row).ToList();
// If you need the results to be in a DataTablestring jsonResults = JsonConvert.SerializeObject(results);DataTable resultsTable = JsonConvert.DeserializeObject<DataTable>(jsonResults);
DataSet dataSet = new DataSet(); //Create a datasetdataSet = _DataEntryDataLayer.ReadResults(); //Call to the dataLayer to return the data
//LINQ query on a DataTablevar dataList = dataSet.Tables["DataTable"].AsEnumerable().Select(i => new{ID = i["ID"],Name = i["Name"]}).ToList();