Question : How can I handle dbNull in Linq?

Dim query = From Trans In MainDS.Transaction.AsEnumerable() _
              Where Trans.CustomerID = _ResultRow("CustomerID") _
              And (Trans.Debit - Trans.Paid) > 0 _
              Order By Trans.SaleDate _
                  Select New With {.Time = Trans.SaleDate, .SaleType = Trans.TransactionType, .TransctionNumber = Trans.TransactionNo, .Debit = Trans.Debit, .Credit = Trans.Credit}

if one of the fields are null I get an error, is there a way around that?

Answer : How can I handle dbNull in Linq?

Hi Mr_Ezi;

You can use the IIf if statement as shown below and return a default value if null/Nothing.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Dim query = From Trans In MainDS.Transaction.AsEnumerable() _
            Where Trans.CustomerID = _ResultRow("CustomerID") _
                  And (Trans.Debit - Trans.Paid) > 0 _
            Order By Trans.SaleDate _
            Select New With _
            {
                .Time = Trans.SaleDate, _
                .SaleType = Trans.TransactionType, _
                .TransctionNumber = Trans.TransactionNo, _
                .Debit = IIf(Trans.Debit != DbNull.Value, Trans.Debit, 0), _
                .Credit = IIf(Trans.Credit != DbNull.Value, Trans.Credit, 0)  _
            }
Random Solutions  
 
programming4us programming4us