C# Linq筛选出具有null或string.empty的行

C# Linq筛选出具有null或string.empty的行,c#,linq,csv,C#,Linq,Csv,我有一个linq查询,其中我正在读取CSV文件的所有行。文件末尾有额外的行,没有数据。我需要过滤掉这些行,以便它只包含包含数据的行 我正在使用下面的查询,但它仍然返回8000行,其中只有52行包含数据 var query = from c in (from line in File.ReadAllLines(excelFile) let transactionRecord = line.Spli

我有一个linq查询,其中我正在读取CSV文件的所有行。文件末尾有额外的行,没有数据。我需要过滤掉这些行,以便它只包含包含数据的行 我正在使用下面的查询,但它仍然返回8000行,其中只有52行包含数据

   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;
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;

不太清楚为什么会这样?有人有什么想法吗?

这将生成一个
IEnumerable
,其中包含至少有一列数据的行(
字符串[]

IEnumerable<string[]> data = 
    from line in System.IO.File.ReadAllLines("")
    let lineData = line.Split(',')
    where lineData.Any(cell => !string.IsNullOrEmpty(cell))
    select lineData;
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;
IEnumerable数据=
从System.IO.File.ReadAllLines(“”)中的第行开始
让lineData=line.Split(','))
其中lineData.Any(单元格=>!string.IsNullOrEmpty(单元格))
选择lineData;

这将给出一个
IEnumerable
,其中包含至少有一列数据的行(
字符串[]

IEnumerable<string[]> data = 
    from line in System.IO.File.ReadAllLines("")
    let lineData = line.Split(',')
    where lineData.Any(cell => !string.IsNullOrEmpty(cell))
    select lineData;
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;
IEnumerable数据=
从System.IO.File.ReadAllLines(“”)中的第行开始
让lineData=line.Split(','))
其中lineData.Any(单元格=>!string.IsNullOrEmpty(单元格))
选择lineData;
这起作用了

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;
这起作用了

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;

为什么不展示一些示例数据?没有它很难知道哪里出了问题。TxnTax被分配到哪里?看起来您已经分配了一个IEnumerable,并在其中迭代了TxnId,但我看不出您在哪里分配了TxnTax。它可能是空值。可能是文件中第行的
。ReadAllLines(excelFile)where!string.IsNullOrWhitespace(line)
您是否真的设置了
c.TxnTax
?如果不是,那么不管怎样,所有结果都将是
null
,因此没有测试点。我相信我的另一个评论就是解决方案,你为什么不展示一些示例数据呢?没有它很难知道哪里出了问题。TxnTax被分配到哪里?看起来您已经分配了一个IEnumerable,并在其中迭代了TxnId,但我看不出您在哪里分配了TxnTax。它可能是空值。可能是文件中第行的
。ReadAllLines(excelFile)where!string.IsNullOrWhitespace(line)
您是否真的设置了
c.TxnTax
?如果不是,那么不管怎样,所有结果都将是
null
,因此没有测试点。无论如何,我相信我的另一个评论就是解决办法