2013-04-10 118 views
0

我有以下設置:LINQ到實體連接查詢

ShoeAreas有列ShoeIdMaterialId。 表Shoes具有列IDStatus

我有一個方法需要一個參數 - materialId,目標是確定ShoeAreas中是否有記錄,其中MaterialId等於像參數那樣傳遞的記錄。並且如果這樣的記錄(或者最有可能的記錄)存在,如果它們相信從Shoes with狀態'=生產'。

我嘗試這樣做:

return shoeService.All(). 
       Join(shoeAreaService.All(), 
       s => s.ID, 
       sa => sa.ShoeId, 
       (s, sa) => (sa.MaterialId == matId)). 
       Any(s => (s.Status == (byte)EntityStatusProd.Production))); 

但我得到了Any..行錯誤說} expected,也這是我第二次LINQ到實體查詢我寫的,所以我懷疑,如果是語法問題或查詢自己錯了。

+1

有適當的實體,您應該能夠使用導航屬性,而不是加入的。 – MarcinJuraszek 2013-04-10 07:01:13

回答

1

您正在從Join方法(條件爲sa.MaterialId == matId的值)返回IEnumerable<bool>。創建匿名類型將舉行雙雙加盟實體,而不是:

return shoeService.All() 
      .Join(shoeAreaService.All(), 
       s => s.ID, 
       sa => sa.ShoeId, 
       (s, sa) => new { s, sa }) // here 
      .Any(x => (x.sa.MaterialId == matId) && 
        (x.s.Status == (byte)EntityStatusProd.Production))); 
+0

謝謝,會用你的答案。 – Leron 2013-04-10 07:16:51

1

你可以試試這個:(LINQ)

from shoe in Shoes 
join shoeArea in ShoesArea on shoe.ID equals shoeArea.ShoeID 
where shoeArea.MeterialID == matID && shoe.Status == (byte)EntityStatusProd.Production 
select new {shoe.ID,shoe.Status}; 
1
return shoeService.All().Any(s => shoeAreaService.All() 
           .Any(sa => sa.MaterialId == matId 
             && s.Id == sa.ShoeId) 
          && s.Status == (byte)EntityStatusProd.Production); 
+1

謝謝,太好了! – Leron 2013-04-10 07:17:09