簡體   English   中英

LINQ到實體聯接查詢

[英]LINQ to Entity join query

我有以下設置:

ShoeAreas有列ShoeIdMaterialId 帶有IDStatus列的桌Shoes

我有一個帶一個參數的方法- materialId和目標是確定是否有在記錄ShoeAreasMaterialId等於一個像傳遞的參數。 並且是否存在這樣的記錄(或最有可能的記錄)是否Shoes with狀態為=生產中的鞋子”中的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 to Entity查詢,因此我懷疑這是語法問題還是查詢本身是錯誤的。

您正在從Join方法中返回IEnumerable<bool> (條件值sa.MaterialId == matId )。 創建匿名類型,該類型將保留兩個已加入的實體:

 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)));

您可以嘗試以下操作:(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};
 return shoeService.All().Any(s => shoeAreaService.All()
                                .Any(sa => sa.MaterialId == matId 
                                        && s.Id == sa.ShoeId)
                          && s.Status == (byte)EntityStatusProd.Production);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM