如何从数据表中选择不在 IEnumerable 中的数据
我有一个 DataTable dbcrs
,我只想获取以下枚举中不的数据:
IEnumerable<Crs> res
注意:两者的键都是 <代码>id。
I have a DataTable dbcrs
and I want to get only the data which is not in the following enumerable:
IEnumerable<Crs> res
Note : the key in both is id
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我的建议:
Here is my suggestion:
首先,您需要使用
AsEnumerable()
来查询 DataTable 的 Rows 集合,然后使用!Contains
as not in ,如下所示:First you need to use
AsEnumerable()
in order to query against the DataTable's Rows collection, then use!Contains
as not in like this:使用 except 和 IEquatable<> 执行此操作的示例如下:
这种方式的好处是您可以定义“等于”的含义,以便仍然可以使用可能具有相同 ID 但不相等的两个列表。
例如,您从两个表中获取数据,因此 Id 可以重复,但其他一些属性定义它们是否实际上相等。
An example of doing this with Except and IEquatable<>
A benefit of this way is that you can define what you mean by "Equals", so that two lists which may have the same ID's but are NOT equal can still be used.
e.g. You get data from two tables, so the Id's can repeat but some other properties define if they are actually equal.
这将返回一行,因为在数据表和数组中都有一个共同的值,即 2。所以输出将是
2,测试
This would return us one row because in Datatable and array both have one value in common that's 2 only. so out put will be
2, Test