SELECT P.ProductId, P.ProductCategoryId, P.ParentProductCategoryId,
P.ProductName, PC.Name AS Category, P.Price, P.ProductYear
FROM dbo.ProductCategory AS PC
INNER JOIN
(SELECT dbo.ProductCategory.ParentProductCategoryId,
dbo.ProductCategory.ProductCategoryId,
dbo.ProductCategory.Name AS CategoryName,
dbo.Product.ProductId,
dbo.Product.Price,
dbo.Product.Name AS ProductName,
dbo.Product.ProductYear
FROM dbo.Product
INNER JOIN dbo.ProductCategory
ON dbo.ProductCategory.ProductCategoryId = dbo.Product.ProductCategoryId
) AS P
ON PC.ProductCategoryId = P.ParentProductCategoryId
1
A
Répondre
1
Je ne suis pas tout à fait sûr de votre mise en page de base de données, mais la déclaration LINQ ressemblerait à quelque chose comme ça ...
YourDataContext db = new YourDataContext();
var query =
from p in db.Products
join pc in db.ProductCategories on p.ProductCategoryId equals pc.ProductCategoryId
select new
{
p.ProductId,
p.ProductCategoryId,
p.ParentProductCategoryId,
p.ProductName,
Category = pc.Name,
p.Price,
p.ProductYear
}
+0
Si vous regardez Attention, vous remarquerez que le questionneur lie les parents ProductCategories aux enfants, puis aux produits. – Benjol
+0
Ah, on dirait que quelqu'un a édité le post et utilisé des blocs de code. Appologies. –
Questions connexes
- 1. Comment écrire une expression pour une propriété linq to sql?
- 2. NOLOCK avec Linq to SQL
- 3. LinQ To SQL avec UPDLOCK
- 4. Comment utiliser LINQ to SQL?
- 5. LINQ to SQL context.GetChanges
- 6. Linq to Sql avec ADO.Net Data Services
- 7. Sql Query to Linq To Sql
- 8. Linq to SQl
- 9. Comment écrire une requête ContainsAll en utilisant LINQ? (C# LINQ-TO-SQL)
- 10. LINQ to SQL
- 11. LINQ To SQL Paging
- 12. linq to sql "Contient"
- 13. LINQ-to-SQL IN()
- 14. Linq-to-SQL ToDictionary()
- 15. LINQ to SQL Particularités
- 16. Classes LINQ to SQL
- 17. LINQ to SQL, Generalization
- 18. Vérifiez la date avec Linq-to-SQL
- 19. Problème avec l'insertion LINQ to SQL. .
- 20. Renvoie un nombre avec linq-to-sql
- 21. Linq to SQL avec Group par
- 22. Tables de consultation avec Linq-to-Sql
- 23. Concurrence avec Linq To Sql Procédures stockées
- 24. LINQ to SQL Insert
- 25. Erreur LINQ to SQL
- 26. LINQ to SQL Groupement
- 27. Associations LINQ to SQL?
- 28. Mappage externe LINQ to SQL
- 29. Version de LINQ to SQL
- 30. LINQ to SQL batch supprimer
vaudraient peut-être, y compris par exemple des résultats que vous voulez ici ... – Benjol