GridView 仅填充 1 个结果

发布于 2025-01-15 16:39:24 字数 1632 浏览 2 评论 0原文

我目前正在努力将数据添加到 GridView。数据来自不同数据库上的 2 个表。目前我可以填充第一个条目,但它不会填充过去的条目。这是代码:

        void FillOrder(int inv)
        {
            var _ord = new OrdersContext();
            var _pro = new ProductContext();

            var qryOrder = (from o in _ord.OrderDetails
                            where o.InvNumberId == inv
                            select new
                            {
                                o.ProductID,
                                o.Quantity
                            }).ToList();
            foreach (var order in qryOrder)
            {
                int prodID = order.ProductID;
                int itemCount = qryOrder.Count;
                var qryProducts = (from p in _pro.Products
                                   where p.ProductID == prodID
                                   select new
                                   {
                                       p.ProductID,
                                       p.ProductName
                                   }).ToList();

                var results = (from t in qryOrder
                               join s in qryProducts
                               on t.ProductID equals prodID
                               select new
                               {
                                   t.ProductID,
                                   t.Quantity,
                                   s.ProductName
                               }).ToList();
                OrderItemList.DataSource = results;
                OrderItemList.DataBind();

            }

        }

任何人都可以帮忙解释为什么它只填充第一个条目吗?

I'm currently working to add Data to a GridView. The data comes from 2 tables that are on different databases. Currently I am able to populate the first entry, but it does not populate past that. here is the code:

        void FillOrder(int inv)
        {
            var _ord = new OrdersContext();
            var _pro = new ProductContext();

            var qryOrder = (from o in _ord.OrderDetails
                            where o.InvNumberId == inv
                            select new
                            {
                                o.ProductID,
                                o.Quantity
                            }).ToList();
            foreach (var order in qryOrder)
            {
                int prodID = order.ProductID;
                int itemCount = qryOrder.Count;
                var qryProducts = (from p in _pro.Products
                                   where p.ProductID == prodID
                                   select new
                                   {
                                       p.ProductID,
                                       p.ProductName
                                   }).ToList();

                var results = (from t in qryOrder
                               join s in qryProducts
                               on t.ProductID equals prodID
                               select new
                               {
                                   t.ProductID,
                                   t.Quantity,
                                   s.ProductName
                               }).ToList();
                OrderItemList.DataSource = results;
                OrderItemList.DataBind();

            }

        }

Can anyone help as to why it's only populating the first entry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2025-01-22 16:39:24

如果涉及的产品数量相对较少(并且由于此查询似乎与一张发票相关,我认为这是正确的),那么您可能可以使用类似下面的代码。

这将消除循环,但 contains 方法可能会生成类似 select ProductID, ProductName from products where ProductID in (,,,,,,) 的 SQL 语句,因此如果参数数量过多,则可能会失败非常大。

var _ord = new OrdersContext();
var _pro = new ProductContext();

var qryOrder = (from o in _ord.OrderDetails
                where o.InvNumberId == inv
                select new
                {
                    o.ProductID,
                    o.Quantity
                }).ToList();

// Get the productIDs 
var productIDS = qryOrder.Select(o=>o.ProductID).Distinct().ToList();

// Get the details of the products used. 
var qryProducts = (from p in _pro.Products
                   where productIDS.Contains(p.ProductID)
                   select new
                   {
                       p.ProductID,
                       p.ProductName
                   }).ToList();

// Combine the two in memory lists
var results = (from t in qryOrder
               join s in qryProducts
               on t.ProductID equals s.ProductID
               select new
               {
                   t.ProductID,
                   t.Quantity,
                   s.ProductName
               }).ToList();

OrderItemList.DataSource = results;
OrderItemList.DataBind();
    
    
    

If the number of products involved is relatively small, (and since this query seems to be relate to one invoice, I would think that is true), then you can probably use something like the code below.

This is removing the loop, but the contains method will probably generate a SQL statement something like select ProductID, ProductName from products where productID in (,,,,,,) so may fail if the number of parameters is extremely large.

var _ord = new OrdersContext();
var _pro = new ProductContext();

var qryOrder = (from o in _ord.OrderDetails
                where o.InvNumberId == inv
                select new
                {
                    o.ProductID,
                    o.Quantity
                }).ToList();

// Get the productIDs 
var productIDS = qryOrder.Select(o=>o.ProductID).Distinct().ToList();

// Get the details of the products used. 
var qryProducts = (from p in _pro.Products
                   where productIDS.Contains(p.ProductID)
                   select new
                   {
                       p.ProductID,
                       p.ProductName
                   }).ToList();

// Combine the two in memory lists
var results = (from t in qryOrder
               join s in qryProducts
               on t.ProductID equals s.ProductID
               select new
               {
                   t.ProductID,
                   t.Quantity,
                   s.ProductName
               }).ToList();

OrderItemList.DataSource = results;
OrderItemList.DataBind();
    
    
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文