简体   繁体   English

我的linq查询无法正常工作,我在做什么错?

[英]My linq query is not working as expected, what im i doing wrong?

I have a datatable i create after reading in a txt file, once i have that datatable, i need to remove rows based on 1 column and a list of values List returnClass(), but after some testing, i found that my LINQ is removing more than expected... so not sure what im doing wrong with the code below. 我在读取txt文件后创建了一个数据表,一旦有了该数据表,就需要删除基于1列和值列表List returnClass()的行,但是经过一些测试,我发现我的LINQ正在删除超出了预期...所以不确定下面的代码在做什么。

At first i thought everything was good, because i was working with a large number of records and the numbers were going down with every pass, but now that im working with a small file to debug, i find out that its removing more than it should.. 起初,我认为一切都很好,因为我正在处理大量记录,并且每次传递的次数都在减少,但是现在我正在处理一个小的文件进行调试,我发现它删除了更多的东西..

Here is my LINQ query: 这是我的LINQ查询:

// REMOVES ALL RECORDS WITH A CLASS THAT IS NON-LABEL CLASS                    
var query = from r in d.AsEnumerable()
            where !returnClass().Any(r.Field<string>("Column7").Contains)
            select r;

DataTable output = query.CopyToDataTable<DataRow>();
int dtoutputCount = output.Rows.Count;

ToCSV(output, ftype, "filteredclass");

Here is my list: (shorten to make this a simple question) 这是我的清单:(简化一下这个简单的问题)

private List<string> returnClass()
{
    List<string> cl = new List<string>();
    cl.Add("7");
    cl.Add("72");
    return cl;
}

My datatable has 100 rows, the column7 has number in it, type string, i need to find each row that has the exact numbers in my list, so if it finds a 7, i dont want it and if it find a 72 i dont want it. 我的数据表有100行,column7中有数字,键入字符串,我需要在列表中找到具有确切数字的每一行,因此,如果找到7,我不想要它,如果找到72我没有想要它。 BUT if there is a 75, or 17 or 127 those need to stay. 但如果有75、17或127,则需要留下。 And the query above is removing those because it contains the number 7. 上面的查询正在删除那些,因为它包含数字7。

How can i remove based on exact matches? 如何根据完全匹配删除?

I think the problem is that you are actually looking to see if any value in your returnClass() list contains the value found in column7 . 我认为问题在于您实际上正在查看是否在returnClass()列表中的任何值包含在column7找到的值。 Inside your Any function you are using String.Contains , and I'm guessing you expect this is a List.Contains or something along those lines. Any函数内部,您正在使用String.Contains ,并且我猜您希望这是一个List.Contains或类似的东西。

Try this instead (untested): 尝试以下操作(未试用):

// REMOVES ALL RECORDS WITH A CLASS THAT IS NON-LABEL CLASS                    
var query = from r in d.AsEnumerable()
            where !returnClass().Contains(r.Field<string>("Column7"))
            select r;

DataTable output = query.CopyToDataTable<DataRow>();
int dtoutputCount = output.Rows.Count;

ToCSV(output, ftype, "filteredclass");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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