LINQ to DataTable

I have a big DataTable. I want to get a subset of this DataTable represented as a DataTable. In short, how to select specific columns in a DataTable.

I tried something like this, but it does not work ...

DataTable dTable = new DataTable();
...
...
...
        DataTable dt = from field in dTable
                       where field.Field<string>("Manager")
                       where field.Field<string>("Phone")
                       select field;

Maybe my code is wrong, how can I get the β€œmanagers” and β€œPhone” columns from one DataTable to another without looping it?

+5
source share
3 answers

Link to DataTableExtensions -

http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx

Then...

var whatever = dTable.AsEnumerable();

Then for example MSDN ...

var productNames = from products in table.AsEnumerable() 
      select products.Field<string>("ProductName");

/: , , DataTable . , DataTable? , ... . , .

+4

:

  var query = from row in dTable.AsEnumerable()
      select new
      {
         manager = row.Field<string>("Manager"),
         phone = row.Field<string>("Phone")                         
      };
+2

LINQ DataTable Specific DataSet AsEnumerable.

, .

    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    DataColumn dc;
    DataRow dr;
    ds.DataSetName = "products";
    dt.TableName = "product";

    dc = new DataColumn("product_id",long.MaxValue.GetType());
    dt.Columns.Add(dc);

    dc = new DataColumn("product_name");
    dt.Columns.Add(dc);

    dr = dt.NewRow();
    dr["product_id"] = 1;
    dr["product_name"] = "Monitor";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 2;
    dr["product_name"] = "Mouse";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 3;
    dr["product_name"] = "KeyBoard";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 4;
    dr["product_name"] = "LCD";
    dt.Rows.Add(dr);

    ds.Tables.Add(dt);

    IEnumerable<DataRow> objResult1 = from tbl in dt.AsEnumerable()
                                   where tbl.Field<long>(0) <= 2
                                   select tbl;

    Response.Write("<b>Query Results 1</b>");
    foreach (DataRow row in objResult1)
    {
        Response.Write(string.Format("<br/>Product ID: {0} ,  Product Name: {1}", row.Field<long>(0), row.Field<string>(1)));
    }

    IEnumerable<DataRow> objResult2 = from tbl in ds.Tables[0].AsEnumerable()
                                   let product_name = tbl.Field<string>(1)
                                   where product_name.StartsWith("Key")
                                   || product_name.StartsWith("Mo")
                                   select tbl;

    Response.Write("<br/><br/><b>Query Results 2</b>");
    foreach (DataRow row in objResult2)
    {
        Response.Write(string.Format("<br/>Product ID: {0} ,  Product Name: {1}", row.Field<long>(0), row.Field<string>(1)));
    }
+1

All Articles