Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对两个DataTable对象使用Except子句_C#_Sql Server_Linq_Linq To Objects - Fatal编程技术网

C# 对两个DataTable对象使用Except子句

C# 对两个DataTable对象使用Except子句,c#,sql-server,linq,linq-to-objects,C#,Sql Server,Linq,Linq To Objects,我目前想从一个具有不同字段的DataTable对象中选择不同的记录,但我想排除另一个DataTable中存在其CustomerID的记录。或多或少类似于在SQL中使用“Except”或“notin”子句。在SQL中是 SELECT * FROM Table1 WHERE CustomerID Not In (SELECT CustomerID FROM Table2) 我想在另一个数据表中设置结果 这就是我所拥有的: var test = (from dt1 in lastMonthFunde

我目前想从一个具有不同字段的DataTable对象中选择不同的记录,但我想排除另一个DataTable中存在其CustomerID的记录。或多或少类似于在SQL中使用“Except”或“notin”子句。在SQL中是

SELECT * FROM Table1 WHERE CustomerID Not In (SELECT CustomerID FROM Table2)
我想在另一个数据表中设置结果

这就是我所拥有的:

var test = (from dt1 in lastMonthFunded.AsEnumerable() 
select new { CustomerId = dt1.Field<int>("CustomerId"), 
           LoanNumber =  dt1.Field<Int32>("LoanNumber") })
          .Except(from dt2 in lastYearFunded 
          select new {customerNum=dt2.Field<Int32>("LoanNumber")});
var test=(来自lastMonthFunded.AsEnumerable()中的dt1)
选择新的{CustomerId=dt1.Field(“CustomerId”),
LoanNumber=dt1.Field(“LoanNumber”)})
.除(从去年的dt2开始)
选择新的{customerNum=dt2.Field(“LoanNumber”)};
Visual Studio显示以下消息:

错误1实例参数:无法从“System.Data.EnumerablerRowCollection”转换为“System.Linq.IQueryable”

错误2“System.Data.EnumerablerRowCollection”不包含“Exception”和“System.Linq.Queryable”的最佳扩展方法重载的定义。Exception(System.Linq.IQueryable,System.Collections.Generic.IEnumerable)具有一些无效参数


只要您的对象依赖于IQUeryable或基于IEnumerable的类,您就应该能够使用Linq:

var test = from dt1 in lastMonthFunded
           where !(from d2 in LastYearFunded
                   select d2.LoanNumber).Contains(dt1.LoanNumber)
           select dt1;

使用上述LINQ,您将能够获得所有LastMonthFund记录,这些记录在LastMonthFund中不存在。

您的确切问题是什么?谢谢John!我错过了。我发布的查询对我无效。Visual Studio引发以下消息:错误1实例参数:无法从“System.Data.EnumerableRowCollection”转换为“System.Linq.IQueryable”,并添加:错误2“System.Data.EnumerableRowCollection”不包含“Except”的定义和最佳扩展方法重载“System.Linq.Queryable.Except”(System.Linq.IQueryable,System.Collections.Generic.IEnumerable)“有一些无效的参数请将这些信息添加到您的问题中,并显示出现问题的行。请添加此代码的解释。另外,请说明您是如何编写此代码的。这将对本读者和未来的读者有所帮助。