c# - 过滤表中不包含 List<string> 中的项目的 DataTable

标签 c# linq

我在查询 DataSet.Tables[0] 和删除不符合列表要求的行时遇到了一些问题。

//This is my list
var values = new List<string> {"Test", "Test2"};

// Now I just query the DataSet for anything that doesnt match this list
var query = from x in ds.Tables[0].AsEnumerable()
            from b in values
            where !x.Field<string>("ColumnName").Contains(b)
            select x;

这有效并返回结果,但它返回 2 x 组相同的行(我假设是因为没有连接)。

我怎样才能得到这些行的不同值?

最佳答案

听起来你可能想要:

var query = from x in ds.Tables[0].AsEnumerable()
            where !values.Any(b => x.Field<string>("ColumnName").Contains(b))
            select x;

换句话说,找到 values 中的任何值中都不存在“ColumnName”字段值的所有行。

关于c# - 过滤表中不包含 List<string> 中的项目的 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8575255/

相关文章:

c# - LINQ:根据重量/大小将列表分成几组

c# - 如何使用 c#` 将 pdf 文件从合法格式调整为字母格式

c# - 我如何在 linq 中对非字符串字段使用过滤器?

c# - mailto 的更好替代方案

c# - 将 CollectionBase 转换为 List 或可与 Linq 一起使用的数据类型

c# - 使用 LINQ 从两个 IEnumerables 中选择

c# - 将信号从一个 winforms 应用程序发送到另一个

c# - ImapX 无法从我的应用程序登录到本地 hMailServer,但 Thunderbird 可以

c# - 使用 linq 时,在 .ForEach() 之前调用 .Any() 有什么好处吗?

c# - 使用 LINQ 从两个不同的列表创建对,其中条目具有相同的属性