在查询中多次使用相关实体
Use related entity multiple times in query
我正在尝试将两个 table 的列表合并到我制作的通用 class 的一个列表中。
我的问题是如何在不为每个字段调用 firstOrDefault 的情况下从相关的 table 中获取字段,请参阅示例代码,我需要一个非常长的字段 Price 和 Cost,我有一个还有一些事情要做...
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})
我将使用的方法(在 C# 中)首先为相关商店的 ItemStore 记录生成查询,例如
var itemStores = myDbContext.ItemStores.Where(i => i.StoreNo = GlobalValues.StoreID);
然后加入你已有的项目:
return items.Join(itemStores, i => ItemId, is => is.ItemID, (i, is) => new ItemViewBasic {etc });
在@Jeroen-Vannevel 在关于使用 let 关键字的评论中得到提示后,我想出了如何去做。我post把它放在这里供以后参考..
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})
我正在尝试将两个 table 的列表合并到我制作的通用 class 的一个列表中。
我的问题是如何在不为每个字段调用 firstOrDefault 的情况下从相关的 table 中获取字段,请参阅示例代码,我需要一个非常长的字段 Price 和 Cost,我有一个还有一些事情要做...
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})
我将使用的方法(在 C# 中)首先为相关商店的 ItemStore 记录生成查询,例如
var itemStores = myDbContext.ItemStores.Where(i => i.StoreNo = GlobalValues.StoreID);
然后加入你已有的项目:
return items.Join(itemStores, i => ItemId, is => is.ItemID, (i, is) => new ItemViewBasic {etc });
在@Jeroen-Vannevel 在关于使用 let 关键字的评论中得到提示后,我想出了如何去做。我post把它放在这里供以后参考..
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})