0

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).

maccettura
  • 10,514
  • 3
  • 28
  • 35
Karu
  • 13
  • 4
  • 2
    Provide an example of _domain.Name_ and _domaintype.ToString_ values – Steve Apr 04 '18 at 21:15
  • 1
    What is the value of `domain.Name`? `domaintype.ToString`? What is the value of `x.Text` that you are expecting to match both `Where` clauses? – mjwills Apr 04 '18 at 21:15
  • 1
    You are checking if the `Text` property be equal to `domain.Name` **AND** contain a completely separate property (`domainType.ToString()`). Can you confirm that _any_ of your data matches **both** the criteria – maccettura Apr 04 '18 at 21:15
  • I added some more info! @maccettura Yup, 100% sure the criteria I search for, and the data inside the CSV are the same. – Karu Apr 04 '18 at 21:25
  • @Karu Well based on your edit you can clearly see why your application is not working. Look at your logic again. How can a single property (`Text`) be **equal** to "Respect" **AND** Contain "antenataal" – maccettura Apr 04 '18 at 21:25
  • 1
    What is your Text field supposed to be? If it is the entire line then you will fail the first test, if it is just one field you cannot pass both tests. – Paul Gibson Apr 04 '18 at 21:27
  • 1
    @maccettura Ooooooohhhhh. Shit. I have tried looking for a way to load a whole row into x, but so far, I haven't found anything. – Karu Apr 04 '18 at 21:27
  • @Karu if you are parsing CSV data, use a CSV parser. Don't roll your own logic when entire libraries are dedicated to the nuances of CSV parsing. Once you have a well formed structure (i.e all the fields separated into their own properties) you can _easily_ apply whatever logic on whichever properties in whichever combination – maccettura Apr 04 '18 at 21:28

1 Answers1

1

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

M Bakardzhiev
  • 632
  • 5
  • 13
  • Your answer technically will help solve the OP's issue, but based on other information the _real_ issue is that OP is not parsing the CSV successfully so they are only dealing with the raw line/row. It might improve your answer quite a bit by offering help or suggestions with the CSV parsing. That way you are comparing individual properties, not using string.Contains (which _could_ be problematic) – maccettura Apr 04 '18 at 21:57