-3

I would like some help, preferably sample code and a walkthrough on how I can lookup values between two data tables and create a new datatable with the merged values. I was advised that I need to use LINQ but I couldn't understand the examples that I saw online for using SQL to LINQ.

My requirement is to load the contents of two excel files and create a new report containing the missing values. I can do this in excel using vlookup. What I have achieved so far is to load the two excel sheet data into 2 data tables. I am aware we can use datasets as well but I used this method.

So my sample dt1 would look like the one below:

enter image description here

dt2 as below:

enter image description here

I want to get the respective designations from dt2 into dt1 under the designation column, therefore final output to look like below:

enter image description here

EDIT 1:

private void MergeDatatable(string excelFilepath1, string excelFilepath2)
{
    string excelConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" +
                                    excelFilepath1 + "'; Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1;'";
    string excelSql = "select * from [Sheet1$]";

    DataTable dt1 = new DataTable();
    DataTable dt2 = new DataTable();

    using (OleDbDataAdapter adap= new OleDbDataAdapter(excelSql, excelConString))
    {
         adap.Fill(dt1);
    }

    string excelConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" +
                             excelFilepath2 + "'; Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1;'";
    string excelSql = "SELECT * FROM [Sheet1$]";

    using (OleDbDataAdapter adap = new OleDbDataAdapter(excelSql, excelConString))
    {
         adap.Fill(dt2);
    }
            
    var joineddt = from tp in ds.Tables
    join mp in ds.Tables(dt2) on tp.emp_id equals mp.emp_id
    select new
    {
        //my fields here
    };
}
Diego Montania
  • 322
  • 5
  • 12
svb
  • 85
  • 2
  • 10
  • Looks like duplicate http://stackoverflow.com/questions/665754/inner-join-of-datatables-in-c-sharp – Ilia Maskov Apr 30 '15 at 05:58
  • 1
    Well sorry but the problem starts here: "I would like some help, preferably sample code and a walkthrough on how ..." this is not only to broad, you are asking us to do your job or find you a tool that does - both are considered OT on this site - so please either explain what you tried so far and where your problem is or go get a good book/tutorial on the basics and research a bit first – Random Dev Apr 30 '15 at 06:24
  • 1
    @CarstenKönig, were you given a book as a toddler and asked to be able to speak? or were you taught your A,B,C's first? If people could just buy books and learn, then don't you think the purpose of forums or communities such as this would be defeated? Obviously, you'd need samples or a helping hand at times. How did you come to the conclusion that by giving me a sample code it would solve my work or you'd be doing my work? Ever considered that it could be something that I could build on?? I did say that I know I need to use LINQ and that I couldn't understand the examples on msdn. – svb Apr 30 '15 at 07:34
  • 1
    @CarstenKönig, I agree that maybe my searches weren't efficient but look at how agent5566 pointed me to something that I can atleast work on. If that also, doesn't help for whatever reason, am I supposed to not ask out of fear of being criticized? I am happy to be provided with constructive criticism and if thats what you intended, then I am sorry but forums such as these have helped me learn and unlearn a lot of things that books haven't. I am a novice, so please cut me some slack. I don't even know what OT means but hope I can ask now? – svb Apr 30 '15 at 07:39
  • well as I seem to have hit a nerve here: agent5566 did point you do a *good* question you could have found on your own (most likely on the very site you used to write this question in the sidebar) - for the rest: you did not even try to come up with some code of your own - just just *demand LINQ* and give a short sample - please just look around in the FAQ here - I am not trying to offend you - I just wanted to let you know why I flagged this for closing – Random Dev Apr 30 '15 at 08:10
  • look I even help you out with the FAQ: look at these please: https://stackoverflow.com/help/how-to-ask - https://stackoverflow.com/help/on-topic – Random Dev Apr 30 '15 at 08:12
  • @CarstenKönig, Its not that you hit a nerve, don't worry :) I am okay with tough love, but I didn't write code cos I couldn't understand what was on msdn. Maybe its cos its too much information. Its just like a previous post I posted here and was introduced to TPL. Got an URL to a solution, did that help me solve the code issue, yes. Did I understand everything, No? Did I try msdn, hoping I'd understand, Yes. Did I understand?, No :) It's probably going to be the next post. I am happy to tag you on it if I can. You will probably see that I am doing what I can to the best of my ability. – svb Apr 30 '15 at 08:21

1 Answers1

2
//ds = your DataSet
var joinedTableResult = from emp1 in ds.dt1
join emp2 in ds.dt2 on emp1.Employee_ID equals emp2.Employee_ID
select new
{
    Employee_ID = emp1.Employee_ID,
    Department = emp1.Department,
    Tenure = emp1.Tenure,
    Designation = emp2.Designation
};
Diego Montania
  • 322
  • 5
  • 12
  • Hi @Sanka Bulathgama. This was helpful. I am sure I can work on this. Just one clarification, can I store the result into a new dt directly i.e. Datatable joinedDt or should I just assign the joinedTableResult to the source property of the new datatable? – svb Apr 30 '15 at 07:45
  • there are different ways of doing this. I have created a sample code for you. https://onedrive.live.com/redir?resid=677aadcc2039c53b%2136766 – Sanka Bulathgama Apr 30 '15 at 10:24
  • I've tried to follow the example but I got the error Could not find an implementation of the query pattern for source type 'System.Data.DataTable'. 'Join' not found. I've added the changes to the main section of this post – svb May 02 '15 at 14:14