Ric*_*ddy 2 c# entity-framework webforms
我有一个简单的foreach循环,它遍历我存储在用户篮子中的productID,并从数据库中查找产品的详细信息.
从我的代码中可以看出,我目前所拥有的将返回屏幕上的最后一项 - 因为变量在循环中被覆盖.我希望能够连接这个,以便我只能在购物篮中显示商品的详细信息.
我知道我可以做一些非常简单的事情,比如只在我使用的转发器中存储ProductIDs,并且onitemdatabound在那里调用数据库但是我想在可能的情况下只进行一次数据库调用.
目前我有以下内容(从示例中删除了复杂的连接,但如果这很重要,请告诉我):
IQueryable productsInBasket = null;
foreach (var thisproduct in store.BasketItems)
{
productsInBasket = (from p in db.Products
where p.Active == true && p.ProductID == thisproduct.ProductID
select new
{
p.ProductID,
p.ProductName,
p.BriefDescription,
p.Details,
p.ProductCode,
p.Barcode,
p.Price
});
}
BasketItems.DataSource = productsInBasket;
BasketItems.DataBind();
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
听起来你真的想要这样的东西:
var productIds = store.BasketItems.Select(x => x.ProductID).ToList();
var query = from p in db.Products
where p.Active && productIds.Contains(p.ProductID)
select new
{
p.ProductID,
p.ProductName,
p.BriefDescription,
p.Details,
p.ProductCode,
p.Barcode,
p.Price
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1355 次 |
| 最近记录: |