简体   繁体   中英

Filter a DataTable where the table does not contain items in List<string>

I am having a few problems quering a DataSet.Tables[0] and removing rows that do not meet the critira of a List.

//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;

This works and returns the results but it is returning 2 x sets of the same rows (I assume because there is no join).

How can I just get Distinct values of these rows?

It sounds like you probably want:

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

In other words, find all the rows where the "ColumnName" field value isn't present in any of the values in values .

也许您正在寻找的是DistinctIntersect方法而不是“.Contains”?

You don't have to join with your values list:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM