Wednesday, 26 March 2014

.Net Tips : Fill DataTable from another DataTable using LINQ with C#.Net and VB.Net example

You can fill DataTable from another DataTable using LINQ after doing LINQ operation.

This would be useful in condition like you need to perform some operation on DataTable using LINQ and get back result into another 'DataTable'. Means, you can filter and projection on one DataTable and get result in another DataTable.

We can achieve this by using 'CopyToDataTable' method of LINQ query result variable.

Here is example for this.
In this example we are taking one DataTable which contains product information like ID, name, etc. Now we get only those products which ID is greater than one by LINQ operation on this table and get result in another table using 'CopyToDataTable' method of LINQ query result variable.


C#. Net Example :
    private DataTable GetProducts()
    {
        DataTable dt = new DataTable();
        DataColumn dc;
        DataRow dr;

        dt.TableName = "product";

        dc = new DataColumn("product_id", int.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);

        return dt;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtProd = GetProducts();
        var obj = (from m in dtProd.AsEnumerable() where m.Field<int>("product_id") > 1 select m);
        DataTable dt = obj.CopyToDataTable();
        Response.Write("a");

    }

VB.Net Examples :
    Private Function GetProducts() As DataTable
        Dim dt As New DataTable()
        Dim dc As DataColumn
        Dim dr As DataRow

        dt.TableName = "product"

        dc = New DataColumn("product_id", Integer.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)

        Return dt
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim dtProd As DataTable = GetProducts()
        Dim obj = (From m In dtProd.AsEnumerable() Where m.Field(Of Integer)("product_id") > 1 Select m)
        Dim dtResult As DataTable = obj.CopyToDataTable()
    End Sub

Output : 
.Net Tips : Fill DataTable from another DataTable using LINQ


Below are the books that you would like :

18 comments:

  1. Infycle Technologies, the best software training institute in Chennai offers the leading Python course in Chennai for tech professionals, freshers, and students at the best offers. In addition to the Python course, other in-demand courses such as Data Science, Cyber Security, Selenium, Oracle, Java, Power BI, Digital Marketing also will be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7504633633 to get more info and a free demo.

    ReplyDelete
  2. Learn power bi classes online to master data visualization, dashboards, and real-time business insights.

    ReplyDelete
  3. Mulesoft developer training is perfect for gaining hands-on experience in building integration solutionsmulesoft developer training

    ReplyDelete
  4. ServiceNow admin training seems like a great step toward building a stable IT career. Thanks for breaking it down so simply.
    servicenow admin training

    ReplyDelete
  5. Looking for salesforce cpq training online, this content really helped me understand the learning path and flexibility.salesforce cpq training online

    ReplyDelete
  6. MuleSoft API training is essential in today’s integration-driven world. This article explains the concept and its importance very well.mulesoft api training

    ReplyDelete
  7. MuleSoft API training is crucial for modern development. Thanks for covering this topic.mulesoft api training

    ReplyDelete
  8. Great post! Our dell boomi training and placement
    teaches how to build, deploy, and monitor integrations using AtomSphere. Hands-on labs and real-world exercises ensure learners develop skills that employers highly value.

    ReplyDelete
  9. Choosing the right approach for training servicenow can help you master workflow automation and platform management.training servicenow

    ReplyDelete