Posts Tagged ‘Datatable’

h1

Select distinct rows from Datatable using Linq

July 30, 2010

var distinctRows = (from DataRow dRow in dTable.Rows select new {col1=dRow[“dataColumn1”],col2=dRow[“dataColumn2”]}).Distinct();

foreach (var row in distinctRows)
{
var value1=row.col1.ToString();
var value2=row.col2.ToString();
}

don’t forget to add System.Linq Class

Advertisements
h1

Convert XML to Datatable

July 19, 2010

DataSet ds= new DataSet();
DataTable dtComp = new DataTable();
System.IO.StringReader rdr;
string sXML =  xmldoc.ToString();
rdr = new System.IO.StringReader(sXML);
ds.ReadXml(rdr);
if ( ds.Tables.Count > 0)
{
dtComp = ds.Tables[0];
}

Convert XML to Datatable with XML Schema : Function

private DataSet getDataSet(string sXML, string xSchema)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
System.IO.StringReader rdr;
rdr = new System.IO.StringReader(sXML);
System.IO.StringReader rdrSchema;
rdrSchema = new System.IO.StringReader(xSchema);
ds.ReadXmlSchema(rdrSchema);
ds.ReadXml(rdr);
if (ds.Tables.Count > 0)
{
return ds;
}
return ds;
}