2014-04-29 4 views
-1

나는 linq을 배우려고 노력하고 있지만 힘들다. 누군가가 ... Linq는이 SQL 쿼리를 변환 할 수이 SQL 쿼리를 Linq로 변환

또한
SELECT 
    sl.Id, 
    sl.Name, 
    t.Quantity 
FROM StorageLocations sl 
LEFT JOIN PartLocations pl ON sl.Id = pl.StorageLocationId 
LEFT JOIN (SELECT 
      PartLocationId, 
      SUM(CASE WHEN TransactionTypeId = 2 THEN Quantity * -1 
      ELSE Quantity END) AS Quantity 
      FROM Transactions WHERE Active = '1' 
      GROUP BY PartLocationId) t ON pl.Id = t.PartLocationId 
WHERE t.Quantity < 1 OR t.Quantity is null 

내가 MVC C# 프로그램을 만드는 중이라서, 나는이 LINQ 문 주위에 사용하여 상황에 맞는 문을 넣어해야합니까?

+1

정말로 직접 해결책을 요구하는 대신 스스로 linq 문을 작성하고 게시해야합니다. – Niklas

답변

1

이 쿼리에서 많은 일이 발생했습니다. 궁금한 점이 있으면 알려주십시오.

using(var context = new YourContext()) 
{ 
    var query = from sl in context.StorageLocations 
       from pl in context.PartLocations.Where(x => sl.Id == x.StorageLocationId).DefaultIfEmpty() 
       from t in 
       (
       from tr in context.Transactions 
       where tr.Active == "1" 
       group tr by tr.PartLocationId into g 
       select new { 
        PartLocationId = g.Key, 
        Quantity = g.Sum(y => y.TransactionTypeId == 2 ? (y.Quantity * -1) : y.Quantity) 
       } 
       ).Where(x => pl.Id == x.PartLocationId).DefaultIfEmpty() 
       where t.Quantity < 1 || t.Quantity == null 
       select new 
       { 
       sl.Id, 
       sl.Name, 
       t.Quantity 
       }; 
    var result = query.ToList(); 
} 
관련 문제