RedArmy
RedArmy

Reputation: 475

How To Compare Table With Datatable or Datable A with Datatable B and update Table by c#

I want To Best and Fast Solution.

Dt_AllPage with 2 column and meny rows . column 0 = ModelRam and Column 1 = Price. string. [dbo].[TBL_Sku_RAM] with columns : [ID]int,[SKU]string,[ModelRam]string,[Price]string

I want to compare Each rows'column ModelRam Of Dt_AllPage with Each Rows' colmun Sku of TBL_Sku_RAM .

for this, select from tbl_sku_ram and append to Dt_SelectTBlRAM and compare two DataTable with names : Dt_AllPage with Dt_SelectTBlRAM

and if Exist regex one rows of Dt_AllPage in Dt_SelectTBlRAM then, get ID of Dt_SelectTBlRAM and get Price of this rowexist Dt_AllPage and update columns : [ModelRam],[Price] of [dbo].[TBL_Sku_RAM];

example:

in Dt_AllPage

ModelRam                         Price
--------------                 ----------
4GB DDR3 PC3                      1,000
8GB DDR3 PC3L Geil 1.35V          5,000

in [dbo].[TBL_Sku_RAM]

 ID       Sku                     ModelRam                         Price
 --- -----------------          --------------                 ----------
  1 8GBDDR3 pc3L-1600               null                           null
  2  1GBDDR3-1066                   null                           null
  3  2GBDDR2-800                    null                           null
  4  1GBDDR2-667                    null                           null

output:

in [dbo].[TBL_Sku_RAM]

 ID       Sku                     ModelRam                         Price
 --- -----------------          --------------                 ----------
  1 8GBDDR3 pc3L-1600            8GB DDR3 PC3L Geil 1.35V          5,000
  2  1GBDDR3-1066                   null                           null
  3  2GBDDR2-800                    null                           null
  4  1GBDDR2-667                    null                           null

code :

1)

   for (int i = 0; i < Dt_AllPage.Rows.Count; i++)
   {
       Model_name = Dt_AllPage.Rows[i][0].ToString();

       DataSet Ds_SelectTBlRAM = DAL.SelectTBlRAM();
       DataTable Dt_SelectTBlRAM = Ds_SelectTBlRAM.Tables[0];

       for (int RowTBL = 0; RowTBL < Dt_SelectTBlRAM.Rows.Count; RowTBL++)
       {
           if (Regex.IsMatch(Dt_SelectTBlRAM.Rows[RowTBL][1].ToString(), Model_name))
           {
             
               int ModelId = (int)Dt_SelectTBlRAM.Rows[RowTBL][0];
               string Price = Dt_AllPage.Rows[i][1].ToString();

               DAL.Update_Price_Model(Model_name, Price, ModelId);

           }


       }
   }

2)

for (int i = 0; i < .Rows.Count; i++)
       {
           Model_name = Dt_AllPage.Rows[i][0].ToString();
    
           DataSet Ds_SelectTBlRAM = DAL.SelectTBlRAM();
           DataTable Dt_SelectTBlRAM = Ds_SelectTBlRAM.Tables[0];
    
    
           DataRow row1 = Dt_SelectTBlRAM.AsEnumerable().FirstOrDefault(r => r.Field<string>("SKU").Contains(Model_name));
           foreach (DataRow row2 in Dt_SelectTBlRAM.Rows)
           {
               if (row1 != null)
               {
                   int Model_ID = (from DataRow DR in Dt_SelectTBlRAM.Rows
                                   where (string)DR["SKU"] == Model_name
                                   select (int)DR["ID"]).FirstOrDefault();
               }
    
           }
    
    
    
           string Price = Dt_AllPage.Rows[i][1].ToString();
           Model_name = Dt_AllPage.Rows[i][0].ToString();
    
       }

Upvotes: 1

Views: 87

Answers (1)

cyrus2500
cyrus2500

Reputation: 428

for (int i = 0; i < Dt_AllPage.Rows.Count; i++)
       {
           Model_name = Dt_AllPage.Rows[i][0].ToString();

           DataSet Ds_SelectTBlRAM = DAL.SelectTBlRAM();
           DataTable Dt_SelectTBlRAM = Ds_SelectTBlRAM.Tables[0];


           DataRow row1 = Dt_SelectTBlRAM.AsEnumerable().FirstOrDefault(r => r.Field<string>("SKU").Contains(Model_name));
           foreach (DataRow row2 in Dt_SelectTBlRAM.Rows)
           {
               if (row1 != null)
               {
                   int Model_ID = (from DataRow DR in Dt_SelectTBlRAM.Rows
                                   where (string)DR["SKU"] == Model_name
                                   select (int)DR["ID"]).FirstOrDefault();
               }

           }



           string Price = Dt_AllPage.Rows[i][1].ToString();
           Model_name = Dt_AllPage.Rows[i][0].ToString();

       }

Upvotes: 1

Related Questions