LINQ to SQL Join and If Condition



我有下面的查询,如果bstock不为空,并且bstock.pricebstock.PPrice是一个可以为空的doubel?)不为空的话,我想在这里设置价格。联接是左外部联接。有人能帮我吗?

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();

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
                    });

相关内容

  • 没有找到相关文章

最新更新