Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to join 2 datatables on multiple conditions in c# using LINQ
C#
var resultingTable1 = from t1 in dtresult.AsEnumerable()
                      join t2 in DB1.AsEnumerable()
                        on new { 
                          LoginID = t1.Field<string>("LoginID"), 
                          ReportDate = t1.Field<DateTime>("ReportDate") } 
                        equals new { 
                          LoginID = t2.Field<string>("Login ID"), 
                          ReportDate = t2.Field<DateTime>("Login time") }
                        select new { t1, t2 };

getting invalid cast error

What I have tried:

C#
var resultingTable1 = from t1 in dtresult.AsEnumerable()
                                      join t2 in DB1.AsEnumerable()
                                          on new { LoginID = t1.Field<string>("LoginID"), ReportDate = t1.Field<DateTime>("ReportDate") } equals new { LoginID = t2.Field<string>("Login ID"), ReportDate = t2.Field<DateTime>("Login time") }
                                      select new { t1, t2 };
Posted
Updated 15-Dec-16 0:31am
v2
Comments
Andy Lanng 14-Dec-16 6:58am    
Could you show us some of the data in t1 and t2, please. It looks like t2.Field<DateTime> isn't getting a DateTime from the field.
Ehsan Sajjad 14-Dec-16 11:52am    
how the data looks like in the datatables?
Jon McKee 14-Dec-16 20:38pm    
As Andy mentioned, could you post your data table classes? The code posted should work fine assuming it's setup correctly.

1 solution

I got its answer..
Changed datatype from datetime to string..

Thanks all..

I am using dynamic datatables. So I am not using any table classes.
 
Share this answer
 
v2

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