Abhilash J A

Abhilash J A

  • 535
  • 2.4k
  • 596.3k

How to merge one DataTable to another DataTable in c#?

Oct 20 2016 10:52 PM
Hello Everyone, 
 
I have two datatable from this query:-
 
  1. string sql = "SELECT * FROM Tble_UserSetUp inner join Tble_Login on Tble_UserSetUp.Id = Tble_Login.UserId WHERE Tble_Login.UserId='" + Userid + "';select count(*) as docCount from Tble_UserSetUp inner join Tble_Documents on Tble_Documents.UserId = Tble_UserSetUp.Id where Tble_Documents.UserId ='" + Userid + "'";  
  2.   
  3. DataTable dt = new DataTable();  
  4. DataTable dt1 = new DataTable();  
  5.   
  6.  da.Fill(ds);  
  7.  dt = ds.Tables[0];  
  8.  dt1 = ds.Tables[1];  
dt is getting all userdetails and dt1 is getting only count.
How can I merge dt1 inside dt? There is UserSetUp model class
contain int type countID, how to assign count value to countID variable
from dt1?
 
  1. public partial class UserSetUp  
  2.     {  
  3.  public Int64 countID { getset; }   
  4. }  
  5.   
  6. var userList = from myRow in dt.AsEnumerable()  
  7.                                where myRow.Field<bool>("IsActive1") == true   
  8.                                select myRow;  
  9.                 List<Model.UserSetUp> objUserSetUp = userList.Select(m => new Model.UserSetUp()  
  10.                 {  
  11.                     Id = m.Field<Int64>("Id"),  
  12.                     FirstName = m.Field<string>("FirstName"),  
  13.                     SurName = m.Field<string>("SurName"),  
  14.                     Computer_Name = m.Field<string>("Computer_Name"),  
  15.                     IP_Address = m.Field<string>("IP_Address"),  
  16.                     LogInTime = m.Field<string>("LogInTime"),  
  17.                     UserName = Decrypt(m.Field<string>("UserName")),  
  18.                     Password = Decrypt(m.Field<string>("Password")),  
  19.                     login_Id = m.Field<Int64>("login_Id"),  
  20.                     UserType = "Documents Scanned",  
  21.   
  22.                     countID = ? /*How can I assign dt1 value inside this variable?*/  
  23.   
  24.                 }).ToList();  
 
 

Answers (2)