2010-11-14 9 views
0

J'ai une requête qui a les éléments suivantsLINQ requête imbriquée

var myvar = from table in MyDataModel 
      where..... 
      select new MyModel 
      { 
        modelvar1 = ..., 
        modelvar2 = (from..... into anothervar) 
      } 

Ce que je veux faire est d'avoir modelvar2 une jointure entre le résultat que je reçois actuellement de UneAutreVariable avec une autre table MyDataModel.

Merci

+0

Pourriez-vous être plus précis? Pourquoi ça ne marche pas maintenant? – svick

+0

cela ne fonctionne pas car il me manque la partie qui vient après la parenthèse pour calculer la jointure. Je suis en train de le faire pour le moment mais si vous pouvez me montrer la syntaxe, ce serait génial. – user471807

Répondre

2

La parenthèse ressemble plus à un sous-requête qu'une jointure. C'est comme ça que vous faites une jointure.
Exemples de tables du AdventureWorks database.

using (DataClasses1DataContext context = new DataClasses1DataContext()) 
{ 
    // If you have foreign keys correctly in your database you can 
    // join implicitly with the "dot" notation. 
    var myvar = from prod in context.Products 
       where prod.ListPrice < 10 
       select new 
       { 
        Name = prod.Name, 
        Category = prod.ProductSubcategory.ProductCategory.Name, 
       }; 

    // If you don't have foreign keys you need to express the join 
    // explicitly like this 
    var myvar2 = from prod in context.Products 
       join prodSubCategory in context.ProductSubcategories 
       on prod.ProductSubcategoryID equals prodSubCategory.ProductSubcategoryID 
       join prodCategory in context.ProductCategories 
       on prodSubCategory.ProductCategoryID equals prodCategory.ProductCategoryID 
       where prod.ListPrice < 10 
       select new 
       { 
        Name = prod.Name, 
        Category = prodCategory.Name, 
       }; 

    // If you REALLY want to do a subquery, this is how to do that 
    var myvar3 = from prod in context.Products 
       where prod.ListPrice < 10 
       select new 
       { 
        Name = prod.Name, 
        Category = (from prodSubCategory in context.ProductSubcategories 
           join prodCategory in context.ProductCategories 
           on prodSubCategory.ProductCategoryID equals prodCategory.ProductCategoryID 
           select prodCategory.Name).First(), 
       }; 

    // If you want to get a list from the subquery you can do like this 
    var myvar4 = from prodCategory in context.ProductCategories 
       select new 
       { 
        Name = prodCategory.Name, 
        Subcategoreis = (from prodSubCategory in context.ProductSubcategories 
            where prodSubCategory.ProductCategoryID == prodCategory.ProductCategoryID 
            select new { prodSubCategory.ProductSubcategoryID, prodSubCategory.Name }).ToList(), 
       }; 

} 
+0

Merci. Ce que je veux vraiment, c'est une sous-requête, comme l'exemple 3. Au lieu de prendre le premier élément comme vous l'avez montré, je veux joindre le résultat de la sous-requête avec une autre table. Comment ça marche? – user471807

+0

@ user471807, j'ai ajouté un quatrième exemple où une liste est renvoyée à partir de la sous-requête. Attention, cela peut parfois créer des requêtes SQL trop coûteuses si vous l'imbriqué trop profondément, surveillez-les dans SQL Profiler pour être sûr d'avoir une requête sensée. –

0
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry] 
FROM [Orders] AS [t0] 
INNER JOIN ([Order Details] AS [t1] 
    INNER JOIN [Products] AS [t2] ON [t1].[ProductID] = [t2].[ProductID]) ON [t0].[OrderID] = [t1].[OrderID] 

peut être écrire comme

from o in Orders 
join od in (
    from od in OrderDetails join p in Products on od.ProductID equals p.ProductID select od) 
    on o.OrderID equals od.OrderID 
select o 
Questions connexes