How to handle multiple result sets that return NULL in LINQ to SQL? RRS feed

  • Question

  • Hi

    I have a stored procedure that returns multiple result sets  (see below)

    select a, count(*)
    from table
    where condition = something
    group by a
    compute sum(count(*))

    if there are no record that matches with the condition, the first result set return 0 record, the second result set returns NULL (which is correct).   The question is how do I handle the NULL value when the stored procedure is called from LINQ to SQL? 

    BTW, I have no problem when the stored procedure returns me the count() and the total if there are records match with the condition.


    Wednesday, April 15, 2009 5:43 AM

All replies

  • My C# code listed below.  It throws an exception at the last line because the results.GetResult<Int32>() returns NULL result:


    IMultipleResults results = db.GetDeliveredLeadsRS(CustomerNumber, System.DateTime.Now);


    var DeliveredLeadsList = results.GetResult<DeliveredLeadsResult>().ToList();


    int TotalLeads = results.GetResult<Int32>().SingleOrDefault();  <-- Exception Thrown

    Wednesday, April 15, 2009 5:48 AM
  • Can you try:

    int TotalLeads = result.GetResult<Int32?>() ?? 0;

    Wednesday, April 15, 2009 6:00 AM