как написать с LinQ для SQL? - PullRequest
1 голос
/ 19 июня 2009
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 Ответ

1 голос
/ 19 июня 2009

Я не совсем уверен в разметке вашей базы данных, но оператор LINQ будет выглядеть примерно так ...

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 
    } 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...