Lining up data for calculations in C#

I currently get information into 2 datatable that I want to perform calculations on.  This data has dates in the series and I want to be sure that the data is aligned so that the calcs are run on the same numbers of the corresponding dates of the two datatables.  Is there an easier way of doing this rather than iterating through all the rows and doing a check to see if the dates match up?  Would it be easier to do the calcs if the information was held in an array?
coderbluesAsked:
Who is Participating?
 
Jon500Connect With a Mentor Commented:
In general, what you want to do here is add your two datatable objects to the same dataset object then create a relation between the two datatables, which effectively does a .Net INNER JOIN on them. You can then iterate through the dataset knowing that all the dates have been joined-up:

ds.Relations.Add("MyDataRelation", _
   ds.Tables("OrderDetails").Columns("Date"), _
   ds.Tables("CustomerInfo").Columns("Date"))

I hope this helps point you in the right direction.

Regards,
Jon500
0
 
SameerJagdaleCommented:
you can take a look at datatable.select() method that helps to filter out data.
http://msdn.microsoft.com/en-us/library/aa325668(VS.71).aspx
 
0
 
coderbluesAuthor Commented:
Jon500: Thank you. Can you point me to an example where two datatables are joined into the same dataset object?  I am pulling this information from an external datasource and the two datatables are currently not related.  

In other words if I have two datatables (dt1 & dt2) how can I combine them into the same dataset (ds)?  Still kind of new to C#.  Thanks.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Jon500Commented:
Can you please tell me which version of C# you're using so I don't end up giving you 2.0 code when you might still be using 1.1?
0
 
coderbluesAuthor Commented:
Visual Studio 2008
0
 
Jon500Connect With a Mentor Commented:
To add a table to any existing dataSet object, use this code:

   dataSet.Tables.Add(table);

In this example, table is your existing table object.

Once you do that, you create your relation using the Relations object I showed above. Then you can iterate through the "joined" table and inspect the columns, iterate through them, etc.

Rather than reinvent this wheel, here is a full example of this:
http://tinyurl.com/yzotbqt

I hope this helps.

Regards,
Jon500
0
All Courses

From novice to tech pro — start learning today.