Ask a questionAsk a question
 

AnswerUnion and Intersection with Linq

  • Monday, November 02, 2009 4:26 PMbelgie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have two datasets, and each contains only one table with one integer column.
    I want to merge the two datasets, sometimes using Intersection and sometimes using Union.
    I am using VB.NET VS2008.

    What is the best way to do this? 
    Thanks
    Bill
    bill

Answers

  • Tuesday, November 03, 2009 2:08 PMbelgie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The book LINQ for Dummies has the following which works great (replace Union with Intersect if you want).

    Even though its C#, its easy enough to convert to VB



    // Perform the query.
    IEnumerable<DataRow> DataOut =
       TestMe. Tables[“ Table1” ] . AsEnumerable() .
          Union(TestMe. Tables[“ Table2 ”] . AsEnumerable( ) ,
             DataRowComparer. Default) .
          OrderBy(ThisRow => ThisRow. Field<DateTime>( “ Day” ) ) ;


    bill

All Replies

  • Tuesday, November 03, 2009 2:08 PMbelgie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The book LINQ for Dummies has the following which works great (replace Union with Intersect if you want).

    Even though its C#, its easy enough to convert to VB



    // Perform the query.
    IEnumerable<DataRow> DataOut =
       TestMe. Tables[“ Table1” ] . AsEnumerable() .
          Union(TestMe. Tables[“ Table2 ”] . AsEnumerable( ) ,
             DataRowComparer. Default) .
          OrderBy(ThisRow => ThisRow. Field<DateTime>( “ Day” ) ) ;


    bill