0

I have this Linq expression that works well. What is the right approach to test if it has returned anything?

        internal static void getCreditNotes(string invoiceId, out DataTable creditNotes,out double total)
        {
            total = 0;
            creditNotes = new DataTable();
            var q = dt.AsEnumerable().
                Where(r => 
                    (string) r["invoiceId"] == invoiceId 
                    && (string) r["TransactionType"] == "CreditMemo");
            creditNotes = q.CopyToDataTable();         
            total = creditNotes.AsEnumerable().Sum(r => (double) r["TransactionAmount"]);

This code breaks on line 9 (q.CopyToDataTables) when there are no rows. I could use a try catch but that seems a bit of an overkill.

Edited by PerplexedB

2
Contributors
4
Replies
17
Views
2 Years
Discussion Span
Last Post by PerplexedB
0

Well... I'd rather check if q is empty. q being empty is not an error condition.
Thanks.

0

right.... That's what I was missing: if (What?). The answer is if(q.Count()==0) of course.

This question has already been answered. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.