2013-08-19 101 views
0

我想知道我怎麼可以這樣寫查詢:轉換查詢拉姆達

var query = from p in context.DimProduct 
     from psc in context.DimProductSubcategory 
     // on psc.ProductCategoryKey equals pc.ProductCategoryKey 
     where psc.EnglishProductSubcategoryName == subCategoryName 
       && psc.ProductSubcategoryKey == p.ProductSubcategoryKey 
     select new DimProductDTO 
        { 
         ProductKey = p.ProductKey, 
         ProductSubcategoryKey = p.ProductSubcategoryKey, 
         EnglishProductName = p.EnglishProductName, 
         Size = p.Size, 
         StandardCost = p.StandardCost 
        }; 

我嘗試了一些疑問,但沒有成功。我的問題是我不知道如何訪問DimProduct和DimProductSubcategory。

有什麼建議嗎?

+0

你又問了什麼問題?我們如何知道你在說什麼? lambda問題在哪裏? –

+0

由於MarcinJuraszek解決了問題 - 我不知道如何編寫「Where支架」來訪問這兩個表。 – user2004403

回答

1
context.DimProduct 
     .SelectMany(p => new { p, psc = context.DimProductSubcategory }) 
     .Where(x => x.psc.EnglishProductSubcategoryName == subCategoryName 
       && x.psc.ProductSubcategoryKey == x.p.ProductSubcategoryKey) 
     .Select(x => new DimProductDTO { 
         ProductKey = x.p.ProductKey, 
         ProductSubcategoryKey = x.p.ProductSubcategoryKey, 
         EnglishProductName = x.p.EnglishProductName, 
         Size = x.p.Size, 
         StandardCost = x.p.StandardCost }) 

但是,你不能從DimProductSubcategory選擇什麼,所以我認爲同樣可以使用Any()擴展方法來完成:

context.DimProduct 
     .Where(x => context.DimProductSubcategory 
          .Any(y => y.EnglishProductSubcategoryName == subCategoryName 
           && y.ProductSubcategoryKey == x.ProductSubcategoryKey)) 
     .Select(x => new DimProductDTO { 
         ProductKey = x.ProductKey, 
         ProductSubcategoryKey = x.ProductSubcategoryKey, 
         EnglishProductName = x.EnglishProductName, 
         Size = x.Size, 
         StandardCost = x.StandardCost }); 

應該產生在查詢中IN SQL語句。

+0

這是我正在尋找的答案:我不知道如何編寫Where的括號。 – user2004403

1

這是不完全一樣的查詢,但它會產生相同的結果通過內部連接(我認爲是更有效的比交叉連接)

context.DimProduct 
     .Join(context.DimProductSubcategory 
       .Where(x => x.EnglishProductSubcategoryName == subCategoryName), 
      p => ProductSubcategoryKey, 
      psc => ProductSubcategoryKey, 
      (p,psc) => new { p, psc }) 
     .Select(x => new DimProductDTO { 
         ProductKey = x.p.ProductKey, 
         ProductSubcategoryKey = x.p.ProductSubcategoryKey, 
         EnglishProductName = x.p.EnglishProductName, 
         Size = x.p.Size, 
         StandardCost = x.p.StandardCost }) 

而且你原來的查詢可以被改寫爲

var query = from p in context.DimProduct 
      join psc in context.DimProductSubcategory 
       on p.ProductSubcategoryKey equals psc.ProductSubcategoryKey 
      where psc.EnglishProductSubcategoryName == subCategoryName 
      select new DimProductDTO {  
        ProductKey = p.ProductKey, 
        ProductSubcategoryKey = p.ProductSubcategoryKey, 
        EnglishProductName = p.EnglishProductName, 
        Size = p.Size, 
        StandardCost = p.StandardCost 
      }; 

生成的SQL的樣子:

SELECT [t0].[ProductKey], [t0].[ProductSubcategoryKey] 
FROM [DimProduct] AS [t0] 
INNER JOIN [DimProductSubcategory] AS [t1] 
    ON [t0].[EnglishProductSubcategoryName] = [t1].[ProductSubcategoryKey] 
WHERE [t1].[EnglishProductSubcategoryName] = @p0