BLToolkit: comment obtenir le jeu de résultats maître-détail en utilisant LINQ?
-
28-10-2019 - |
Question
Voici un exemple d'utilisation des associations de liste provenant documentation BLToolkit :
from p in db.Product
select new
{
p.OrderDetails.Count,
p.ProductName
};
où
class Product
{
[PrimaryKey, Identity]
public int ProductID;
public string ProductName;
[Association(ThisKey="ProductID", OtherKey="ProductID")]
public List<OrderDetail> OrderDetails;
}
Est-il possible d'obtenir la liste des produits avec des détails de commande pour chaque produit comme celui-ci:
from p in db.Product
select new
{
Details = p.OrderDetails,
p.ProductName
};
Le code ci-dessus jette LinqException avec le message suivant:
Vous ne trouvez pas le convertisseur pour le type 'System.Collections.Generic.List`1 [[OrderDetail, TestProject, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null]].
La solution
Using the normal linq to sql provider this would work fine. With the BLToolkit Linq provider I don't know.
Autres conseils
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).