SQL到Linq转换

时间:2009-07-30 04:51:52

标签: linq linq-to-objects

我有以下的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;

还有其他好办法吗?

由于

1 个答案:

答案 0 :(得分: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)
                                        };