Spafa9
Spafa9

Reputation: 762

Linq filter out rows with null or string.empty

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?

Upvotes: 0

Views: 6047

Answers (2)

Spafa9
Spafa9

Reputation: 762

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;

Upvotes: 2

polkduran
polkduran

Reputation: 2551

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;

Upvotes: 2

Related Questions