我有 2 个数据表,一个是查找列表,另一个是数据。我在表中有许多列保存数据,其中一列是查找数据表的外键列。
我想根据外键列上的 Id 为所有未出现在包含数据的表中的查找行生成另一个列表。
我想使用 linq 查询,或者适用于 2 个数据表的东西
好像我在做一个 SQL NOT IN。
干杯
这是一些数据。我希望新列表中包含第 4 类和第 5 类。
DataTable dtData = new DataTable( "Data" );
dtData.Columns.Add( "Id", Type.GetType( "System.Int32" ) );
dtData.Columns.Add( "CategoryId", Type.GetType( "System.Int32" ) );
dtData.Columns.Add( "Qty", Type.GetType( "System.Int32" ) );
dtData.Columns.Add( "Cost", Type.GetType( "System.Decimal" ) );
dtData.Columns.Add( "TotalCost", Type.GetType( "System.Decimal" ) );
dtData.Columns.Add( "TypeId", Type.GetType( "System.Int32" ) );
dtData.Rows.Clear();
DataRow row = dtData.NewRow();
row["Id"] = 1;
row["CategoryId"] = 1;
row["Qty"] = 3;
row["Cost"] = 237.00;
row["TotalCost"] = 711.00;
row["TypeId"] = DBNull.Value;
dtData.Rows.Add( row );
row = dtData.NewRow();
row["Id"] = 2;
row["CategoryId"] = 1;
row["Qty"] = 5;
row["Cost"] = 45.00;
row["TotalCost"] = 225.00;
row["TypeId"] = DBNull.Value;
dtData.Rows.Add( row );
row = dtData.NewRow();
row["Id"] = 3;
row["CategoryId"] = 3;
row["Qty"] = 30;
row["Cost"] = 1.00;
row["TotalCost"] = 30.00;
row["TypeId"] = 1;
dtData.Rows.Add( row );
row = dtData.NewRow();
row["Id"] = 4;
row["CategoryId"] = 2;
row["Qty"] = 1;
row["Cost"] = 15.00;
row["TotalCost"] = 15.00;
row["TypeId"] = 2;
dtData.Rows.Add( row );
row = dtData.NewRow();
row["Id"] = 5;
row["CategoryId"] = 1;
row["Qty"] = 4;
row["Cost"] = 3.00;
row["TotalCost"] = 12.00;
row["TypeId"] = 2;
dtData.Rows.Add( row );
DataTable dtlookup = new DataTable( "LookUp" );
dtlookup.Columns.Add( "CategoryId", Type.GetType( "System.Int32" ) );
dtlookup.Columns.Add( "Description", Type.GetType( "System.String" ) );
dtlookup.Rows.Clear();
DataRow lup = dtlookup.NewRow();
lup["CategoryId"] = 1;
lup["Description"] = "Category 1";
dtlookup.Rows.Add( lup );
lup = dtlookup.NewRow();
lup["CategoryId"] = 2;
lup["Description"] = "Category 2";
dtlookup.Rows.Add( lup );
lup = dtlookup.NewRow();
lup["CategoryId"] = 3;
lup["Description"] = "Category 3";
dtlookup.Rows.Add( lup );
lup = dtlookup.NewRow();
lup["CategoryId"] = 4;
lup["Description"] = "Category 4";
dtlookup.Rows.Add( lup );
lup = dtlookup.NewRow();
lup["CategoryId"] = 5;
lup["Description"] = "Category 5";
dtlookup.Rows.Add( lup );
var qqq = ( from r in dtlookup.AsEnumerable()
where !dtData.AsEnumerable().Any( b => b["CategoryId"] == r["CategoryId"] )
select r ).ToList();