ObjectSharp Blogs

You are currently viewing

DataSet FAQ

The Place to Go for DataSets


How can I do a "Join" between two DataTables?

It's tempting to think that with objects like “DataView” and methods like “Select” that DataSets would support this option, but the bottom line is that they don't. A DataView is a sorted and/or filtered list of rows from 1 table only. Also, the Select method is on the DataTable and as such - can't look at other tables.

There are a few options to consider.

  1. Do the join in SQL. SQL is good at this kind of operation. It would require you to have a new DataTable with the composed list of columns from the two tables - and a DataAdapter with at least a SelectCommand specified to support the fill. You would perform your join in the SelectCommand.CommandText. If you are also retrieving data into 2 separate DataTables for other reasons, then of course you have some redundant data and also some synchronization issues between the 2 individual tables and the joined table. The joined table will not reflect changes in the 2 base tables until you update the database and re-execute the Fill on the joined table.
  2. Like the above solution, you could create a 3rd table with the composed list of columns in your DataSet but instead of loading from the dataset, you could copy the data from the base tables yourself using the AddRow method. If the two base tables share the same primary key, you could try to “merge” the data from the two DataTable into the 3rd DataTable.
  3. If the two tables to be joined are in a master detail relationship you can use expression columns to lookup data in a parent DataTable or aggregate records from a Child Table. For example if you have a Customer DataTable with a stateId column, which is a foreign key to a State DataTable with stateId and stateName columns, you can add a computed DataColumn to the Customer DataTable with an expression of Parent.stateName. This new column will be kept in sync if the underlying name changes in the State DataTable or if the stateId is changed on the Customer DataTable to point to a different State. Similarly you can look up values on a child DataTable from a parent DataTable but since there can be 1 or more rows you will typically need aggregates like Sum, Avg, Min, Max, etc. in you expression. The DataColumn.Expression property online help is valuable for the types of expressions you can use.

Comments

  • datasetfaq November 9, 2004 1:11 PM

    I've been led to a 4th option by Rocky Lhotka in which a custom business object exposes the columns from the underlying DataTables as properties. The business object use DataRelations to traverse parent/child relationships. It can also simulate expression columns as you require. This is no different than adding columns to the actual DataTable (at design time in VS2005) but seems to be cleaner. Also, using VS2005 B1 it is now easier to bind controls to business objects.
    I've been around the block on this issue and Rocky's way seems like the best. I will point out that a gotcha is that the BindingList (of T) implementation in .NET 2.0 does not support sorting or filtering in Beta1. So if you have a business object Account and implement a collection using Accounts Inherits (BindingList of Account) you don’t get Sorting for free. Therefore to allow a DataGridView to sort the data by clicking the columns or to get the DataConnector to filter the data like a DataView you have to implement the sorting and filtering portion of IBindingList.

  • datasetfaq November 9, 2004 1:34 PM

    Or if you were crazy like me, you could just implement IListSource in your entity class and passthrough the IListSource interface from the dataset - then you can bind to your entity as if it was a dataset.

    To protect the values from change (say for example a validation) you'd have to implement event handlers for the column & row changing/changed events.

  • datasetfaq September 6, 2005 8:04 PM

    I see that this thread is a bit old, but here goes anyway... Perhaps others finding this thread might be interested in an update.

    I've recently completed a series of projects that will do exactly what you're asking for. (It's for .Net v1.1)

    Joining ADO.Net DataTables: http://home.hot.rr.com/graye/Articles/DataTableJoins.htm
    In-Memory SQL Engine: http://home.hot.rr.com/graye/Articles/SQL_Engine.htm

  • datasetfaq October 26, 2005 2:51 PM

    I have a dataset with computed columns.I want to copy computed dataset to new datset with column mapping.Otherwise can we change the order of columns in computed dataset.So that i can pass dataset to report.

  • datasetfaq December 14, 2005 4:27 PM

    I use the JoinView object from Microsoft

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;325682

Anonymous comments are disabled