简体   繁体   中英

Linq multiple where clauses doesn't work

I'm trying to get particular data from an excel sheet (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.

But it comes up empty when I run the code. Only doing either of the where clauses works just fine. 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". 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).

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.

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. I have used TextFieldParser in a small project, it is pretty straight-forward (but don't forget to wrap it in a "using" block).

Parsing CSV files in C#, with header

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