2010-11-14 301 views
0

我有一個具有以下LINQ嵌套查詢

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

我想要做的是有modelvar2是結果我從目前得到anothervar在MyDataModel另一個表之間連接的查詢。

謝謝

+0

你能更具體?爲什麼現在不工作? – svick 2010-11-14 02:03:51

+0

它不工作,因爲我錯過了計算連接的圓括號之後的部分。我現在在盯着它,但如果你能告訴我語法,那真是太棒了。 – user471807 2010-11-14 02:19:10

回答

2

括號看起來更像是一個子查詢而不是連接。這是你如何做一個加入。
來自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

謝謝。我真正想要的是一個子查詢,就像示例3一樣。而不是像您展示的那樣使用第一個元素,我想將子查詢的結果與另一個表結合起來。這是如何運作的? – user471807 2010-11-15 06:31:57

+0

@ user471807,我添加了從子查詢返回列表的第四個示例。請注意,如果您將它嵌入太深,它有時會創建過於昂貴的SQL查詢,請在SQL Profiler中監視它們以確保您獲得了理智的查詢。 – 2010-11-15 07:21:13

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] 

可以寫爲

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