-1

我有如下的sql

SELECT Q.MaterialID AS MaterialID, Q.ProductID AS ProductID, QB.Quantity AS Quantity, 
        Q.ParameterID AS ParameterID, SUM((Q.ParameterValue * Q.Quantity)/Q.TotalTonnes) AS ParameterValue
    FROM @Quality Q
    INNER JOIN @QuantityBreakdown QB 
    ON ((Q.MaterialID = QB.MaterialID) OR (Q.MaterialID IS NULL AND QB.MaterialID IS NULL))
    AND ((Q.ProductID = QB.ProductID) OR (Q.ProductID IS NULL AND QB.ProductID IS NULL))
    GROUP BY Q.MaterialID, Q.ProductID, ParameterID, QB.Quantity

转换为 LINQ ..... 击中 ???

var enumerable = from final in (from q in qualities
                                        from qb in quantityBreakDowns
                                        where q.ProductID == qb.ProductID && q.MaterialID == qb.MaterialID
                                        select new
                                                   {
                                                       q.MaterialID,
                                                       q.ProductID,
                                                       q.ParameterID,
                                                       qb.Quantity,
                                                       ParameterValue = ((q.ProductID*q.Quantity)/q.TotalTonnes)
                                                   }
                                       )
                         group final by new
                                            {
                                                final.MaterialID,
                                                final.ProductID,
                                                final.ParameterID,
                                                ???
                                            }
                         into finalresult select finalresult;

有没有其他的好方法来做到这一点。

谢谢

4

1 回答 1

1

好的解决了这个问题:

from final in
                             (from q in qualities
                              from qb in quantityBreakDowns
                              where q.ProductID == qb.ProductID && q.MaterialID == qb.MaterialID
                              select new
                                         {
                                             q.MaterialID,
                                             q.ProductID,
                                             q.ParameterID,
                                             qb.Quantity,
                                             ParameterValue = ((q.ActualValue*q.Quantity)/q.TotalTonnes)
                                         }
                             )
                         group final by new
                                            {
                                                final.MaterialID,
                                                final.ProductID,
                                                final.ParameterID,
                                                final.Quantity
                                            }
                         into finalresult
                             select new
                                        {
                                            finalresult.Key.MaterialID,
                                            finalresult.Key.ProductID,
                                            finalresult.Key.ParameterID,
                                            finalresult.Key.Quantity,
                                            ActualValue = finalresult.Sum(fq => fq.ParameterValue)
                                        };
于 2009-07-30T06:17:21.770 回答