Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. from dt1 in dsResults.Tables[0].AsEnumerable()
  2. join dt2 in dsResults.Tables[1].AsEnumerable()
  3. on dt1 .Field<decimal>("RecordId") equals dt2.Field<decimal>("RecordId2")
  4. select dt1 ;
  5.  
  6. public class YourObject
  7. {
  8. public string Pror1 {get;set;}
  9. public string Pror2 {get;set;}
  10. }
  11.  
  12.  
  13.  
  14. List<YourObject> result=from row1 in dsResults.Tables[0].AsEnumerable()
  15. join row2 in dsResults.Tables[1].AsEnumerable()
  16. on row1.Field<decimal>("RecordId") equals row2.Field<decimal>("RecordId2")
  17. select new YourObject()
  18. {
  19. Pror1=row1.prop1,
  20. Prop2=row2.prop2,
  21. ......
  22. }.ToList();
  23.  
  24. from row1 in dsResults.Tables[0].AsEnumerable()
  25. join row2 in dsResults.Tables[1].AsEnumerable()
  26. on row1.Field<decimal>("RecordId") equals row2.Field<decimal>("RecordId2")
  27. select new { RowTable1 = row1, RowTable2 = row2}; //anonymous type
  28.  
  29. from dt1 in dsResults.Tables[0].AsEnumerable()
  30. join dt2 in dsResults.Tables[1].AsEnumerable()
  31. on dt1 .Field<decimal>("RecordId") equals dt2.Field<decimal>("RecordId2")
  32. select new
  33. {
  34. Property1 = dt1 .Field<decimal>("RecordId"),
  35. Property2 = dt2 .Field<decimal>("RecordId2")
  36. ......
  37.  
  38. }
  39. ;
  40.  
  41. from dt1 in dsResults.Tables[0].AsEnumerable()
  42. join dt2 in dsResults.Tables[1].AsEnumerable()
  43. on dt1 .Field<decimal>("RecordId") equals dt2.Field<decimal>("RecordId2")
  44. select new
  45. {
  46. dt1,
  47. dt2
  48. }
  49. ;
  50.  
  51. var parent = new DataTable();
  52. parent.Columns.Add("Id", typeof(int));
  53. parent.Columns.Add("Name", typeof(string));
  54.  
  55. var child = new DataTable();
  56. child.Columns.Add("Id", typeof(int));
  57. child.Columns.Add("ParentId", typeof(int));
  58. child.Columns.Add("ChildName", typeof(string));
  59.  
  60. parent.Rows.Add(1, "A");
  61. parent.Rows.Add(2, "B");
  62. child.Rows.Add(1, 1, "a1");
  63. child.Rows.Add(2, 1, "a2");
  64. child.Rows.Add(3, 2, "b1");
  65. child.Rows.Add(4, 2, "b2");
  66.  
  67. var flat = new DataTable();
  68. foreach (DataColumn col in parent.Columns.Cast<DataColumn>()
  69. .Union(child.Columns.Cast<DataColumn>()))
  70. {
  71. if (!flat.Columns.Contains(col.Caption))
  72. flat.Columns.Add(col.Caption, col.DataType);
  73. }
  74.  
  75. var q = from p in parent.AsEnumerable()
  76. join c in child.AsEnumerable() on p.Field<int>("Id") equals c.Field<int>("ParentId")
  77. select new { p, c };
  78.  
  79. foreach (var pc in q)
  80. {
  81. var row = flat.NewRow();
  82. foreach (var cell in pc.p.Table.Columns.Cast<DataColumn>()
  83. .Zip(pc.p.ItemArray, (col, item) => new { col.Caption, item} ))
  84. {
  85. row[cell.Caption] = cell.item;
  86. }
  87. foreach (var cell in pc.c.Table.Columns.Cast<DataColumn>()
  88. .Zip(pc.c.ItemArray, (col, item) => new { col.Caption, item} ))
  89. {
  90. row[cell.Caption] = cell.item;
  91. }
  92. flat.Rows.Add(row);
  93. }
  94.  
  95. //flat.Dump(); // If in linqpad.
  96.  
  97. Form f = new Form();
  98. DataGridView v = new DataGridView();
  99. v.Dock = DockStyle.Fill;
  100. v.DataSource = flat;
  101. f.Controls.Add(v);
  102. f.Show();
  103.  
  104. DataTable targetTable = dsResults.Tables[0].Clone();
  105. var dt2Columns = dsResults.Tables[1].Columns.OfType<DataColumn>().Select(dc =>
  106. new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
  107. targetTable.Columns.AddRange(dt2Columns.ToArray());
  108. var rowData =
  109. from row1 in dsResults.Tables[0].AsEnumerable()
  110. join row2 in dsResults.Tables[1].AsEnumerable()
  111. on row1.Field<decimal>("RecordId") equals row2.Field<decimal>("RecordId2")
  112. select row1.ItemArray.Concat(row2.ItemArray).ToArray();
  113. foreach (object[] values in rowData)
  114. targetTable.Rows.Add(values);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement