1

Hello i trying to transform an SQL query in C# to LinqToSql. My code is getting 2 tables from an SQL Server and put them in a DataSet like that :

private void Valid_Cmd_Btn_Click(object sender, RoutedEventArgs e)
    {
        int numCmd = Convert.ToInt32(NumCmd_TB.Text);

        #region ARTICLES
        comm = new SqlCommand("SELECT * FROM ARTICLES", conn);
        SqlDataAdapter dap = new SqlDataAdapter(comm);
        DataTable dt = new DataTable("ARTICLES");
        dap.Fill(dt);
        #endregion

        #region CMD_DET
        comm = new SqlCommand("SELECT * FROM CMD_DET", conn);
        SqlDataAdapter dap1 = new SqlDataAdapter(comm);
        DataTable dt1 = new DataTable("CMD_DET");
        dap1.Fill(dt1);
        #endregion

        #region CMD_ENT
        comm = new SqlCommand($"SELECT * FROM CMD_ENT WHERE CMD_ENT_ID= {numCmd}", conn);
        SqlDataAdapter dap2 = new SqlDataAdapter(comm);
        DataTable dt2 = new DataTable("CMD_ENT");
        dap2.Fill(dt2);
        #endregion

         DataSet ds = new DataSet();
        ds.Tables.Add(dt);
        ds.Tables.Add(dt1);
        ds.Tables.Add(dt2);
    }

Now i want to do the same with LinqToSql syntax :

private void Valid_Cmd_Btn_Click(object sender, RoutedEventArgs e)
    {
         DataClasses1DataContext dc = new DataClasses1DataContext();
        var articles = from a in dc.ARTICLES
                       select a;
                       // How to put this in a Datable ?

                        DataClasses1DataContext dc1 = new DataClasses1DataContext();
        var det = from d in dc.CMD_DET
                       select d;
                       // How to put this in a Datable ?

                       //...

                       // and then like in Sql query how to get those 2 tables in a Dataset ??
    }
  • Do you still want to use datasets with LinqToSql? – Fabio Apr 07 '19 at 06:00
  • possible duplicate of https://stackoverflow.com/a/14431698/4437464 – Elyas Esna Apr 07 '19 at 11:32
  • Possible duplicate of [Linq on DataTable: select specific column into datatable, not whole table](https://stackoverflow.com/questions/14422655/linq-on-datatable-select-specific-column-into-datatable-not-whole-table) – Elyas Esna Apr 07 '19 at 11:33
  • thank you for those answer but ther's something i don't understand : "DataTable matrix = ... // get matrix values from db" what does it mean ?? –  Apr 14 '19 at 04:38

0 Answers0