R – Subsonic 3 Linq Projection Issue

activerecordlinqsubsonic3

OK I'm banging my head against a wall with this one 😉

Given tables in my database called Address, Customer and CustomerType, I want to display combined summary information about the customer so I create a query to join these two tables and retrieve a specified result.

 var customers = (from c in tblCustomer.All()
                        join address in tblAddress.All() on c.Address equals address.AddressId
                        join type in tblCustomerType.All() on c.CustomerType equals type.CustomerTypeId
                        select new CustomerSummaryView
                                   {
                                       CustomerName = c.CustomerName,
                                       CustomerType = type.Description,
                                       Postcode = address.Postcode
                                   });

  return View(customers);

CustomerSummaryView is a simple POCO

public class CustomerSummaryView
{
    public string Postcode { get; set; }
    public string CustomerType { get; set; }
    public string CustomerName { get; set; }
}

Now for some reason, this doesn't work, I get an IEnumerable list of CustomerSummaryView results, each record has a customer name and a postcode but the customer type field is always null.

I've recreated this problem several times with different database tables, and projected classes.

Anyone any ideas?

Best Answer

I can't repro this issue - here's a test I just tried:

[Fact]
public void Joined_Projection_Should_Return_All_Values() {
    var qry = (from c in _db.Customers
                     join order in _db.Orders on c.CustomerID equals order.CustomerID
                     join details in _db.OrderDetails on order.OrderID equals details.OrderID
                     join products in _db.Products on details.ProductID equals products.ProductID
                     select new CustomerSummaryView
                     {
                         CustomerID = c.CustomerID,
                         OrderID = order.OrderID,
                         ProductName = products.ProductName
                     });

    Assert.True(qry.Count() > 0);

    foreach (var view in qry) {
        Assert.False(String.IsNullOrEmpty(view.ProductName));
        Assert.True(view.OrderID > 0);
        Assert.False(String.IsNullOrEmpty(view.CustomerID));
    }

}

This passed perfectly. I'm wondering if you're using a reserved word in there?