views:

45

answers:

1

I have sql as below

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

conversion to LINQ..... struck at ???

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;

Is there some other good way to do this.

Thanks

+1  A: 

Ok solved this as:

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