Click here to Skip to main content
15,886,045 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two DataTables and I want to display the rows. if both the datatables having the same value, Then mark X in all columns or else select the column with highest value(Eg:DT1: 10,DT2 :5)

Datatable1

id Name Weight
1 Ship 500
2 Train 600
3 Plane 700
4 Car 800
Datatable2

id Name Weight
1 Ship 500
3 Plane 600
4 Car 200
I want the result to be:

Datatable3

id Name Weight Datatable1 Datatable2
1 Ship 500 X X
2 Train 600 X
3 Plane 700 X X
4 Car 800 X
I have tried the below:-

DataTable Datatable3 = (from a in Datatable1.AsEnumerable()
join b in Datatable2.AsEnumerable()
on a["Name"].ToString() equals b["Name"].ToString()
a["Weight"].ToString() equals b["Weight"].ToString() into g
where g.Count() != 1 select a).CopyToDataTable();
dataGrid1.ItemsSource = Datatable3.DefaultView;
Please help me on this. Thanks in advance
Posted

1 solution

If i understand you well, you need to join data on id, then to compare weights:
C#
DataTable Datatable3 = (from a in Datatable1.AsEnumerable()
    join b in Datatable2.AsEnumerable() on a.id equals b.id
    select new{
        Id = a.id,
        Name = a.Name,
        Weight = a.Weight
        Result = a.Weight == b.Weight ? "x" : ""
        }).CopyToDataTable();


[EDIT]
Ok, try something like this:
C#
var qry = from a in dt1.AsEnumerable() join b in dt2.AsEnumerable() on a.Field<int>("id") equals b.Field<int>("id")
                           select new
                           {
                               Id = a.Field<int>("id"),
                               Name = a.Field<string>("Name"),
                               Weight = a.Field<int>("Weight"),
                               Result = a.Field<int>("Weight") == b.Field<int>("Weight") ? "x" : ""
                           };
foreach(var v in qry)
{
    Console.WriteLine ("{0} {1} {2} {3}", v.Id, v.Name, v.Weight, v.Result );
}
 
Share this answer
 
v2
Comments
LAKSHMINARAYANAN E 23-Dec-14 1:15am    
@Maciej Los Yes your right. Name is also an Identity column. We can compare using name also. Now it is throwing conversion error.
Maciej Los 23-Dec-14 1:49am    
"Now it is throwing conversion error." - What line?
LAKSHMINARAYANAN E 23-Dec-14 1:55am    
CopyToDataTable();
Maciej Los 23-Dec-14 2:04am    
What .net framework version have you installed: 3.5, 4.0, 4.5?
[EDIT]
As far as i know, it works perfect with 4.5
CopyToDataTable[^]
LAKSHMINARAYANAN E 23-Dec-14 2:55am    
This is the error message

The type 'AnonymousType#1' cannot be used as type parameter 'T' in the generic type or method 'System.Data.DataTableExtensions.CopyToDataTable<t>(System.Collections.Generic.IEnumerable<t>)'. There is no implicit reference conversion from 'AnonymousType#1' to 'System.Data.DataRow'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900