lkurylo
lkurylo

Reputation: 1651

how to remove data from a dataset table where is relation

I have a dataset with two tables and between these tables there is a relation:

 DataColumn dc1;
        DataColumn dc2;

        dc1 = q.Tables[0].Columns["dateFrom"];
        dc2 = q.Tables[1].Columns["dateFrom"];
        DataRelation dr = new DataRelation("tracker", dc1, dc2, false);

Now I want to remove from the first table all the rows that don't have a data in the second table. How can I do that?

Upvotes: 2

Views: 334

Answers (2)

Sebastian Siek
Sebastian Siek

Reputation: 2075

Why would you do that in the first place? You might lose important data in this case (unless you know it's unnecessary content). I assume that if there's data in the parent table then it must have been added for reason, or there might be a problem in your code. Have a look at that and try to avoid having dirty data.

With regards to your question, I would loop throug the main records and check if there's any parent records. At the same time compare and make sure the data can be deleted.

Hope it helps.

Upvotes: 1

Krishna
Krishna

Reputation: 2481

Not tested, but you may get an idea from the below (you may not even need to set the relation)

var results = from table1 in q.Tables[0].AsEnumerable()
              join table2 in q.Tables[1].AsEnumerable() on table1["dateFrom"] equals table2["dateFrom"]                  
             select new { 
               dateFrom = table1["dateFrom"], 
               // anyother columns                 
              };

Hope this helps and good luck

Krishna

Upvotes: 1

Related Questions