Datatabe (datarow) compares and calculates the intersection, union, and difference sets.

Source: Internet
Author: User
// First, add system. Data. datasetextensions;
Dataset DS =   New Dataset ();
DS. locale = Cultureinfo. invariantculture;
Filldataset (DS );

Datatable contacttable=DS. Tables ["Contact"];

// Create two tables.
Ienumerable < Datarow > Query1 = From contact In Contacttable. asenumerable ()
Where Contact. Field < String > ( " Title " ) =   " Ms. "
Select contact;

Ienumerable < Datarow > Query2 = From contact In Contacttable. asenumerable ()
Where Contact. Field < String > ( " Firstname " ) =   " Sandra "
Select contact;

Datatable contacts1=Query1.copytodatatable ();
Datatable contacts2=Query2.copytodatatable ();

//Find the intersection of the two tables. Calculate the intersection using intersect/Union (Union)/Except T (difference set)
VaR contacts=Contacts1.asenumerable (). Intersect (contacts2.asenumerable (),
Datarowcomparer. Default );

Console. writeline ( " Intersection of contacts tables " );
Foreach (Datarow row In Contacts)
{
Console. writeline ( " ID: {0} {1} {2} {3} " ,
Row [ " Contactid " ], Row [ " Title " ], Row [ " Firstname " ], Row [ " Lastname " ]);
}

The above is the msdnCodeThe following is your own code, which is relatively concise:

Dbhelpersql. connectionstring = Strsiteconnection;
Datatable dtblsitetable = Dbhelpersql. tquery ( " Select * from "   + Strtablename );
Ienumerable < Datarow > Querysite = From contact In Dtblsitetable. asenumerable () Select contact;

dbhelpersql. connectionstring = strmainconnection;
datatable dtblmaintable = dbhelpersql. tquery ( " select * from " + strtablename );
ienumerable datarow > querymain = from contact in dtblmaintable. asenumerable () Select contact;

VaR enumerable0000t=Querysite. copytodatatable (). asenumerable (). Cannot T (querymain. copytodatatable (). asenumerable (), datarowcomparer. Default );
 
Datatable dtblexcept=Enumerableexcept. copytodatatable ();//Dtblexcept is the data table of the difference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.