简体   繁体   English

Linq Multiple where子句不起作用

[英]Linq multiple where clauses doesn't work

I'm trying to get particular data from an excel sheet (csv). 我正在尝试从Excel工作表(csv)中获取特定数据。

var rows = excelsheet
    .Where(x => x.Text == domain.Name 
             && x.Text.Contains(domaintype.ToString()))
    .Select(x => x.Start.Row)
    .ToList();

I want it to filter on the domain name and any cell in that row can have word x.Text.Contains(domaintype.ToString() inside of it. 我希望它对域名进行过滤,并且该行中的任何单元格都可以在其中包含单词x.Text.Contains(domaintype.ToString()

But it comes up empty when I run the code. 但是当我运行代码时,它变成空的。 Only doing either of the where clauses works just fine. 只执行其中任何一个where子句都可以。 But I need both of the clauses at the same time to work. 但是我需要同时使用这两个子句。 I need the row numbers for the code to work. 我需要行号才能使代码正常工作。

Here is an example: 这是一个例子:

"1 ; Respect ; Hielden uw zorgverleners rekening met uw privacy? ; Vraag antenataal 11"

domain.Name is "Respect", domaintype is, in this case, "antenataal". domain.Name是“尊重”, domaintype是,在这种情况下, “antenataal”。 So I want every row number that has these two filters in it. 因此,我希望每个行号中都包含这两个过滤器。

I know that domain.Name has "Respect" in it and domaintype has "antenataal" in it (through debugging). 我知道domain.Name具有“ Respect”, domaintype具有“ antenataalal”(通过调试)。

Instead of 代替

var rows = excelsheet
.Where(x => x.Text == domain.Name 
         && x.Text.Contains(domaintype.ToString()))
.Select(x => x.Start.Row)
.ToList();

Try 尝试

var rows = excelsheet
.Where(x => x.Text.Contains(domain.Name) 
         && x.Text.Contains(domaintype.ToString()))
.Select(x => x.Start.Row)
.ToList();

Basically, your first condition is asking that "1 ; Respect ; Hielden uw zorgverleners rekening met uw privacy? ; Vraag antenataal 11" should be equal to "Respect" instead of containg it, that's why no value is returned. 基本上,您的第一个条件是要求“ 1;尊重; Hielden uw zorgverleners拒绝满足您的隐私?; Vraag附件11”应等于“ Respect”,而不是包含它,这就是为什么不返回任何值的原因。

Regarding parsing of CSV files, there is already a topic on SO that recommends several parsers, you can check it out and pick this that suits you best. 关于CSV文件的解析,SO上已经有一个主题可以推荐多个解析器,您可以将其签出并选择最适合自己的解析器。 I have used TextFieldParser in a small project, it is pretty straight-forward (but don't forget to wrap it in a "using" block). 我在一个小项目中使用了TextFieldParser ,它非常简单(但不要忘记将其包装在“ using”块中)。

Parsing CSV files in C#, with header 用C#解析带头的CSV文件

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

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