//
首先要添加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. 求交集用 Intersect/Union(并集)/Except(差集)
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 " ]);
}
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. 求交集用 Intersect/Union(并集)/Except(差集)
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 " ]);
}
上面为MSDN的代码,下面为自己的代码,相对简洁些:
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 EnumerableExcept = querySite.CopyToDataTable().AsEnumerable().Except(queryMain.CopyToDataTable().AsEnumerable(), DataRowComparer.Default);
DataTable dtblExcept = EnumerableExcept.CopyToDataTable(); // dtblExcept 即为差异的数据表
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 EnumerableExcept = querySite.CopyToDataTable().AsEnumerable().Except(queryMain.CopyToDataTable().AsEnumerable(), DataRowComparer.Default);
DataTable dtblExcept = EnumerableExcept.CopyToDataTable(); // dtblExcept 即为差异的数据表