0

I have a linq query where I am reading all the lines of a CSV the file has extra rows at the end that have no data. I need to filter out those rows so that it only has the rows with data I am using the following query but it still returns like 8000 rows and there are only 52 with data in them.

   var query =
            from c in
                (from line in File.ReadAllLines(excelFile)
                    let transactionRecord = line.Split(',')
                    select new Transaction()
                    {
                        TxnId = transactionRecord[12],

                    })
            where c.TxnTax != string.Empty
            select c;

Not relaly sure why this is happening? Doe anyone have any ideas?

4
  • 2
    Why don't you show some example data? Difficult to know what's wrong without it. Commented Apr 26, 2013 at 13:33
  • Where is TxnTax being assigned? It looks like you've got an IEnumerable<Transaction> with TxnId assigned that you are iterating through, but I don't see where you assign TxnTax anywhere. It could be a null value. Commented Apr 26, 2013 at 13:37
  • 1
    maybe from line in File.ReadAllLines(excelFile) where !string.IsNullOrWhitespace(line) Commented Apr 26, 2013 at 13:38
  • Are you actually even setting c.TxnTax? If not, then it will be null for all results anyway, so no point testing that. I believe my other comment is the solution anyway Commented Apr 26, 2013 at 13:39

2 Answers 2

2

This will give an IEnumerable containing the lines (string[]) having at least one column with data

IEnumerable<string[]> data = 
    from line in System.IO.File.ReadAllLines("")
    let lineData = line.Split(',')
    where lineData.Any(cell => !string.IsNullOrEmpty(cell))
    select lineData;
Sign up to request clarification or add additional context in comments.

Comments

2

This worked

var query =
                    from c in
                        (from line in File.ReadAllLines(excelFile)
                            let transactionRecord = line.Split(',')
                            select new Transaction()
                            {
                                TxnId = transactionRecord[12],

                            })
                    where ((string.IsNullOrEmpty(c.TxnId) == false) && (c.TxnId != "Billing Information|Transaction ID"))
                    select c;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.