BLToolkit: how to get master-detail result set using LINQ?

1.9k views Asked by At

Here is an example usage of list associations taken from BLToolkit documentation:

from p in db.Product
select new
{
  p.OrderDetails.Count,
  p.ProductName
};

where

class Product
{
  [PrimaryKey, Identity]
  public int ProductID;

  public string ProductName;

  [Association(ThisKey="ProductID",  OtherKey="ProductID")]
  public List<OrderDetail> OrderDetails;
}

Is it possible to get list of products with order details for each product like this:

from p in db.Product
select new
{
  Details = p.OrderDetails,
  p.ProductName
};

The code above throws LinqException with the following message:

Cannot find converter for the 'System.Collections.Generic.List`1[[OrderDetail, TestProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' type.

2

There are 2 answers

3
Magnus On BEST ANSWER

Using the normal linq to sql provider this would work fine. With the BLToolkit Linq provider I don't know.

1
Kirkaiya On

While I haven't used the BLToolkit, you might try to use a class you define rather than relying on an anonymous type.

That is, you could define a class called MyOrder that has properties String ProductName and List<OrderDetail> OrderDetails, and then change your linq query to something like:

from p in db.Product
select new MyOrder
{
  OrderDetails = p.OrderDetails,
  ProductName = p.ProductName
};

I can't guarantee that this would work, but it's probably worth a shot (unless someone posts something more definitive).