将SQL转换为LINQ

时间:2009-05-11 23:30:04

标签: linq linq-to-sql

如何将以下内容转换为LINQ

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

上午:

 (from final in (from q in qualities
                            from qb in quantityBreakDowns
                            where q.MaterialID == qb.MaterialID && q.ProductID == qb.ProductID
                            select q)
             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
                                //QB.Quantity AS Quantity ??
                                //SUM((Q.ParameterValue * Q.Quantity)/Q.TotalTonnes) AS ParameterValue ??

                            }

'??' =>如何获得这些。

这是正确的方法吗?

由于

2 个答案:

答案 0 :(得分:4)

我认为我们不应该为您做LINQ查询,而是指向正确的方向来学习LINQ to SQL。

否则您拥有的下一个查询将处于相同的位置。

答案 1 :(得分:0)

这样的事情应该可以解决问题。 我不知道LinqToSql,所以你需要检查如何获取IQuerable对象。否则,这应该指向正确的方向。

var results = from q in myIQuerableObject<Quality>
from qb on qb.MaterialId == q.MaterialId or (qb.MaterialId == null && q.MaterialId == null)
select new {Quality = q, Breakdown = qb}