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 1 Year Ago by PerplexedB

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

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.