views:

859

answers:

2

Hi,

I've got 3 tables in my DB: InvoiceDetailLineType, InvoicingCategory, CalculationRule.

InvoiceDetailLineType has foreign keys to both InvoicingCategory and CalculationRule.

I want to retrieve a list of all InvoiceDetailLineType objects for a given category and include the related CalculationRule and InvoicingCategory objects.

Here's my method to do this:

public List<InvoiceDetailLineType> GetAllForCategory(InvoicingCategory category)
{
    return (from InvoiceDetailLineType lineType in ContextHelper.Entities.InvoiceDetailLineType.Include("CalculationRule").Include("InvoicingCategory")
            where lineType.InvoicingCategory.IdInvoicingCategory == category.IdInvoicingCategory
            select lineType).ToList();
}

I would expect this to give me what I need, but what I get back is an object whose CalculationRule is null. I've made sure that the foreign keys are all set up correctly and that the records exist in the database, so it seems a bit odd.

Am I missing something?

Thanks in advance.

+1  A: 

Ok, I've found a solution:

public List<InvoiceDetailLineType> GetAllForCategory(InvoicingCategory category)
{
    ObjectQuery<InvoiceDetailLineType> lineTypeQuery = ContextHelper.Entities.InvoiceDetailLineType.Include("CalculationRule").Include("InvoicingCategory");
    return lineTypeQuery.Where(t => t.InvoicingCategory.IdInvoicingCategory == category.IdInvoicingCategory).ToList();
}

Still not quite sure why the inital code I had didn't work, if anyone can shed some light for completeness sake, I'd appreciate it.

Veli
UPDATE: I've also discovered that the original code works, as long as I don't explicitly specify the type of 'lineType' as InvoiceDetailLineType. Now I wonder why that is :)
Veli