[英]LINQ to Entity join query
我有以下設置:
表ShoeAreas
有列ShoeId
和MaterialId
。 帶有ID
和Status
列的桌Shoes
。
我有一個帶一個參數的方法- materialId
和目標是確定是否有在記錄ShoeAreas
與MaterialId
等於一個像傳遞的參數。 並且是否存在這樣的記錄(或最有可能的記錄)是否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.