为什么我的过滤器不起作用?d.DefaultView.RowFilter = cls;

我只想对我的数据表应用行过滤器,但它不起作用,没有错误,只是取回相同的数据表。


我尝试从数据表创建一个数据视图以便应用,但这也不会返回过滤后的列表。我不知道为什么下面的代码不起作用..


DataTable d = processFileData(concatFile);

string cls = String.Format("Column6 NOT IN ({0})", String.Join(",", returnClass()));

d.DefaultView.RowFilter = cls;

上面的 cls 变量是我尝试使用的 int 值列表,如下所示:


Column6 NOT IN (75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,281,303,405,406,493,599,601,606,610,626,630,639,640,647,648,651,662,664,671,672,680,689,697,701,703,706,709,712,717,718,724,735,740,741,743,744,767,768,769,770,775,780,791,799,800,801,802,803,805,806,807,808,810,811,812,815,816,817,818,820,836,837,873,874,875,879,881,895,896,897,902,903,904)

我希望有一组新的记录,其中只有那些在我的过滤器中没有条件的记录。原始 DataTable = 34,945 条记录,如果我在文件中手动应用 excel 中的筛选器,预期结果应该是 DataTable = 29,240 条记录。


这是我尝试使用过滤器完成的示例:


原始数据表:


列 1、列 2、列 4、列 5、列 6


你好,今天,食物,乐趣,75


你好,今天,食物,乐趣,75


你好,今天,食物,乐趣,79


你好,今天,食物,乐趣,79


你好,今天,食物,乐趣,79


你好,今天,食物,乐趣,100


你好,今天,食物,乐趣,101


你好,今天,食物,乐趣,700


你好,今天,食物,乐趣,750


你好,今天,食物,乐趣,749


你好,今天,食物,乐趣,755


你好,今天,食物,乐趣,799


你好,今天,食物,乐趣,799


你好,今天,食物,乐趣,804


根据 Column6 值过滤后的预期数据表:


列 1、列 2、列 4、列 5、列 6


你好,今天,食物,乐趣,100


你好,今天,食物,乐趣,101


你好,今天,食物,乐趣,700


你好,今天,食物,乐趣,750


你好,今天,食物,乐趣,749


你好,今天,食物,乐趣,755


你好,今天,食物,乐趣,804


哈士奇WWW
浏览 114回答 2
2回答

HUWWW

您可以为此使用 linq 而不是 DataViewList<int> filter = new List<int>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,281,303,405,406,493,599,601,606,610,626,630,639,640,647,648,651,662,664,671,672,680,689,697,701,703,706,709,712,717,718,724,735,740,741,743,744,767,768,769,770,775,780,791,799,800,801,802,803,805,806,807,808,810,811,812,815,816,817,818,820,836,837,873,874,875,879,881,895,896,897,902,903,904&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp;DataTable output = dt.AsEnumerable().Where((row,index) => !filter.Contains(index)).CopyToDataTable();希望这能回答你的问题

波斯汪

&nbsp;// list of values to be filtered&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<int> filter = new List<int>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,281,303,405,406,493,599,601,606,610,626,630,639,640,647,648,651,662,664,671,672,680,689,697,701,703,706,709,712,717,718,724,735,740,741,743,744,767,768,769,770,775,780,791,799,800,801,802,803,805,806,807,808,810,811,812,815,816,817,818,820,836,837,873,874,875,879,881,895,896,897,902,903,904&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp;// LINQ statement to do filtering&nbsp; IEnumerable<DataRow> unmatchingRows = from DataRow row in dt.Rows&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where !filter.Contains((int)row[0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select row;&nbsp; // add to a new datatable&nbsp; DataTable output = dt.Clone();&nbsp; foreach (DataRow item in unmatchingRows)&nbsp; {&nbsp; &nbsp; &nbsp; output.ImportRow(item);&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP