我正在尝试将两个表的列表放入我创建的通用类的一个列表中。
我的问题是如何在不为每个字段调用 firstOrDefault 的情况下从相关表中获取字段,请参阅示例代码,我需要一个非常长的字段来表示价格和成本,我还有一些事情要做。 .
Dim items As IQueryable(Of ItemMain)
Return items.Where(Function(i) i.Status > -1).Select(Function(x) New ItemViewBasic() With {.ItemID = x.ItemID,
.Name = x.Name,
.BarcodeNumber = x.BarcodeNumber,
.Price = x.ItemStores.Where(Function(itemStore) itemStore.StoreNo = GlobalValues.StoreID).FirstOrDefault().Price,
.Cost = x.ItemStores.Where(Function(itemStore) itemStore.StoreNo = GlobalValues.StoreID).FirstOrDefault().Cost})
最佳答案
在从@Jeroen-Vannevel 那里得到关于使用 let 关键字的评论中的提示后,我想出了如何去做。我把它贴在这里以供将来引用..
Return (From i In items Let itemStore = i.ItemStores.Where(Function(its) its.StoreNo = GlobalValues.StoreID And its.Status > -1).FirstOrDefault() Where i.Status > -1
Select New ItemViewBasic With {.ItemID = i.ItemID,
.Name = i.Name,
.Cost = itemStore.Cost,
.BarcodeNumber = i.BarcodeNumber,
.OnHand = itemStore.OnHand,
.Department = itemStore.DepartmentID,
.Price = itemStore.Price,
.ModalNumber = i.ModalNumber})
关于.net - 在查询中多次使用相关实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29881657/