使用另一个查询中的值更新现有列表值

时间:2010-09-29 16:57:00

标签: linq

我有一个linq语句,它调用存储过程并返回项目和描述的列表。

像这样;

var q = from i in doh.usp_Report_PLC() where i.QTYGood == 0 orderby i.PartNumber select new Parts() { PartNumber = i.PartNumber, Description = i.Descritpion.TrimEnd() };

然后我有另一个SQL语句,它返回每个项目的订单和交货日期的数量。 Parts类有两个其他属性来存储它们。如何使用其他两个值更新现有零件清单,以便有一个包含所有四个值的零件清单?

更新

以下代码现在显示结果。

                var a = from a1 in db.usp_Optos_DaysOnHand_Report_PLC()
                    where a1.QTYGood == 0
                    orderby a1.PartNumber
                    select new Parts() { PartNumber = a1.PartNumber, Description = a1.Descritpion.TrimEnd() };

            var b = from b1 in db.POP10110s
                    join b2 in db.IV00101s on b1.ITEMNMBR equals b2.ITEMNMBR
                    //from b3 in j1.DefaultIfEmpty()
                    where b1.POLNESTA == 2 && b1.QTYCANCE == 0
                    group b1 by new { itemNumber = b2.ITMGEDSC } into g
                    select new Parts() { PartNumber = g.Key.itemNumber.TrimEnd(), QtyOnOrder = g.Sum(x => Convert.ToInt32(x.QTYORDER)), DeliveryDue = g.Max(x => x.REQDATE).ToShortDateString() };

            var joinedList = a.Join(b,
                    usp => usp.PartNumber,
                    oss => oss.PartNumber,
                    (usp, oss) =>
                         new Parts
                         {
                             PartNumber = usp.PartNumber,
                             Description = usp.Description,
                             QtyOnOrder = oss.QtyOnOrder,
                             DeliveryDue = oss.DeliveryDue
                         });

            return joinedList.ToList();

1 个答案:

答案 0 :(得分:0)

假设您的“其他SQL语句”返回PartNumberQuantityDeliveryDate,您可以将列表合并为一个:

var joinedList = q.Join(OtherSQLStatement(), 
                        usp => usp.PartNumber,
                        oss => oss.PartNumber,
                        (usp, oss) => 
                             new Parts
                                    {
                                        PartNumber = usp.PartNumber,
                                        Description = usp.Description,
                                        Quantity = oss.Quantity,
                                        DeliveryDate = oss.DeliveryDate
                                    }).ToList();

您实际上可以组合查询并在一个连接和投影中执行此操作:

var joinedList = doh.usp_Report_PLC().
                     Where(i => i.QTYGood == 0).
                     OrderBy(i => i.PartNumber).
                     Join(OtherSQLStatement(), 
                          i => i.PartNumber,
                          o => o.PartNumber,
                          (i, o) => 
                               new Parts
                                    {
                                        PartNumber = i.PartNumber,
                                        Description = i.Description,
                                        Quantity = o.Quantity,
                                        DeliveryDate = o.DeliveryDate
                                    }).ToList();

再次说明:我假设你在两个返回的集合中都有PartNumber来识别属于哪个项目。

修改

在这种情况下,LINQ Query语法可能更具可读性:

var joinedList = from aElem in a
                 join bElem in b
                 on aElem.PartNumber equals bElem.PartNumber into joinedAB
                 from abElem in joinedAB.DefaultIfEmpty()
                 select new Part
                            {
                                PartNumber = aElem.PartNumber,
                                Description = aElem.Description,
                                DeliveryDue = abElem == null ? null : abElem.DeliveryDue,
                                QtyOnOrder = abElem == null ? null : abElem.QtyOnOrder
                            };

您的DeliveryDueQtyOnOrder可能是可以为空的。如果不是,请使用默认值替换空值。例如。如果您在b中没有该元素,并且希望QtyOnOrder在结果列表中为0,请将该行更改为

QtyOnOrder = abElem == null ? 0 : abElem.QtyOnOrder