LINQ to SQL 连接和 If 条件

发布于 2024-11-15 09:03:20 字数 1233 浏览 2 评论 0原文

我有下面的查询,如果 bstock 不为 null 并且 bstock.Pricebstock.Price 为 null-),我想在其中设置价格能加倍吗?)不为空。该连接是左外连接。有人可以帮助我吗?

var bstocks = (from p in qry
                             join bstock in bstockRepository.Select() on p.StockCode equals bstock.StockCode
                             into J1
                             from bstock in J1.DefaultIfEmpty()
                             select new
                             {
                                 p.StockCode,
                                 p.Description,
                                 p.ListPrice,
                                 p.Price =  bstock.Price != null ? bstock.Price : p.Price,
                                 p.QuantityOnHand ,
                                 p.Cube,
                                 p.ShippingFormat,
                                 p.Weight,
                                 p.NextShipment,
                                 p.NextShipment2,
                                 p.NextShipmentQuantity,
                                 p.NextShipment2Quantity,
                                 Bstock = p.Bstock
                             }
                              ).AsQueryable();

I have the query below where I want to set the price if bstock is not null and bstock.Price (bstock.Price is a null-able doubel? ) is not null. The join is a left outer join. Can someone help me?

var bstocks = (from p in qry
                             join bstock in bstockRepository.Select() on p.StockCode equals bstock.StockCode
                             into J1
                             from bstock in J1.DefaultIfEmpty()
                             select new
                             {
                                 p.StockCode,
                                 p.Description,
                                 p.ListPrice,
                                 p.Price =  bstock.Price != null ? bstock.Price : p.Price,
                                 p.QuantityOnHand ,
                                 p.Cube,
                                 p.ShippingFormat,
                                 p.Weight,
                                 p.NextShipment,
                                 p.NextShipment2,
                                 p.NextShipmentQuantity,
                                 p.NextShipment2Quantity,
                                 Bstock = p.Bstock
                             }
                              ).AsQueryable();

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

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

发布评论

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

评论(1

过期情话 2024-11-22 09:03:20

当您运行左连接时,bstock 在您的查询中可以为 null。所以你需要考虑到:

Price =  bstock != null && bstock.Price != null ? bstock.Price : p.Price,

结果:

var bstocks = (from p in qry
                join bstock in bstockRepository.Select() on 
                    p.StockCode equals bstock.StockCode into J1
                from bstock in J1.DefaultIfEmpty()
                select new
                {
                        p.StockCode,
                        p.Description,
                        p.ListPrice,
                        Price =  bstock != null && bstock.Price != null ? bstock.Price : p.Price,
                        p.QuantityOnHand ,
                        p.Cube,
                        p.ShippingFormat,
                        p.Weight,
                        p.NextShipment,
                        p.NextShipment2,
                        p.NextShipmentQuantity,
                        p.NextShipment2Quantity,
                        Bstock = p.Bstock
                    });

bstock can be null in your query, as you are running left join. So you need to account for that:

Price =  bstock != null && bstock.Price != null ? bstock.Price : p.Price,

Result:

var bstocks = (from p in qry
                join bstock in bstockRepository.Select() on 
                    p.StockCode equals bstock.StockCode into J1
                from bstock in J1.DefaultIfEmpty()
                select new
                {
                        p.StockCode,
                        p.Description,
                        p.ListPrice,
                        Price =  bstock != null && bstock.Price != null ? bstock.Price : p.Price,
                        p.QuantityOnHand ,
                        p.Cube,
                        p.ShippingFormat,
                        p.Weight,
                        p.NextShipment,
                        p.NextShipment2,
                        p.NextShipmentQuantity,
                        p.NextShipment2Quantity,
                        Bstock = p.Bstock
                    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文