Left outer join relation between Datasets
I have two datasets 开发者_如何学Gonamed dataset1,dataset2. I want put the left outer join relation between them.
eg:dataset1 left outer join dataset2
Your question is a bit unclear so there are at least two possible answers.
If you do indeed have two different DataSet
s and they need to stay as separate DataSet
s then you could use LINQ (with Linq to DataSet) to create a left outer join. Here are a couple of blog post explaining how to create left outer joins using LINQ (one and two). Your code will probably look something along the lines of
var joinResult = from ds1Row in dataset1.Tables["some table"]
join ds2Row in dataset2.Tables["some other table"]
on ds1Row.Field<T>("some column") equals ds2Row.Field<T>("some other column") into tmpResult
from resultRow in tmpResult.DefaultIfEmpty()
select new
{
//select whatever information you want
column1 = ds1Row.Field<T>("name column"),
column2 = ds1Row.Field<T>("name column"),
column3 = ds2Row.Field<T>("name column")
};
If you really meant doing a join between two DataTable
s or you can combine two DataSets into a single DataSet using the Merge method and then create a relationship between any two tables. Your code will look something along the lines of:
mergedDataset.Relations.Add("relation name",
mergedDataset.Tables["some table"].Columns["some column"],
mergedDataset.Tables["some other table"].Columns["some other column"]);
You can only create a DataRelation between two tables in the same DataSet. What you will need to do is use the Merge method to bring the two datasets together e.g.
DataSet1.Merge(DataSet2)
Then you can create a DataRelation between the two tables by running
DataSet1.Relations.Add(new DataRelation("myRelationship",DataTable1.Columns("ID"),DataTable2.Columns("ID")))
INNER JOIN, LEFT OUTER JOINS and RIGHT OUTER JOIN for DataTables
the link has a good vb program that does outer join. You can use it if you use in your vb.net , check it out.
DataTable Relational Operators in C# - JOIN Method
both codes are self explanatory and as Duncan said you can only do this using data tables are not using two separate data sets.
If you just need the result, I would suggest using LINQ to DataSets, which allows you to use any of the LINQ operators to query and join two DataTables/DataSets.
Erick
精彩评论