2016-03-03 145 views
0

我有这样的事情:如何翻译T-SQL SUM(),以LINQ和()

SQL query

,我需要重新编写这个查询到LINQ。

我想:

var EachProduct = (from b in _context.ReleasePlans 
            join a in _context.Products 
             on b.ProductProductId equals a.ProductId 
            where b.DepartmentDepartmentId == departmentId 
            select new { ProductId = b, ProductName = a }); 

可是我该怎么办SUM()和DATEPART()函数中的LINQ?

UPD:使用lambda表达式进行查询。但问题依然如故

var EachProduct = _context.ReleasePlans 
      .Where(b => b.DepartmentDepartmentId == departmentId) 
      .Join(_context.Products, a => a.ProductProductId, b => b.ProductId, 
       (b, a) => new {ProductId = b, ProductName = a}); 
+2

本季度,参见[如何从当前日期使用C#得到当前季度(http://stackoverflow.com/questions/21266857/how-to-get -current-quarter-from-current-date-using-c-sharp),对于Sum,你将需要使用linq组,就像在sql中一样。 – stuartd

+2

实体框架还是LINQ-to-SQL? – xanatos

+0

是的,我在这个项目中使用实体框架 – Eluvium

回答

1
class Program 
{ 
    public class ReleasePlan 
    { 
     public int ProductProductId { get; set; } 
     public int Amount { get; set; } 
     public DateTime DateTime { get; set; } 
    } 

    public class Product 
    { 
     public int ProductId { get; set; } 
     public string ProductName { get; set; } 
     public int DepartmentDepartmentId { get; set; } 
    } 

    static void Main() 
    { 
     var products = new List<Product> 
     { 
      new Product {ProductId = 1, ProductName = "1", DepartmentDepartmentId = 1}, 
      new Product {ProductId = 2, ProductName = "2", DepartmentDepartmentId = 1}, 
      new Product {ProductId = 3, ProductName = "3", DepartmentDepartmentId = 1}, 
     }; 
     var releasePlans = new List<ReleasePlan> 
     { 
      new ReleasePlan {ProductProductId = 1, Amount = 1, DateTime = DateTime.Now}, 
      new ReleasePlan {ProductProductId = 1, Amount = 1, DateTime = DateTime.Now}, 
      new ReleasePlan {ProductProductId = 1, Amount = 1, DateTime = DateTime.Now.AddMonths(-5)}, 

      new ReleasePlan {ProductProductId = 2, Amount = 2, DateTime = DateTime.Now}, 
      new ReleasePlan {ProductProductId = 2, Amount = 2, DateTime = DateTime.Now}, 
      new ReleasePlan {ProductProductId = 2, Amount = 2, DateTime = DateTime.Now.AddMonths(-5)}, 

      new ReleasePlan {ProductProductId = 3, Amount = 3, DateTime = DateTime.Now}, 
      new ReleasePlan {ProductProductId = 3, Amount = 3, DateTime = DateTime.Now}, 
      new ReleasePlan {ProductProductId = 3, Amount = 3, DateTime = DateTime.Now.AddMonths(-5)}, 

     }; 


     var amountByProducts = from rp in releasePlans 
      join p in products 
       on rp.ProductProductId equals p.ProductId 
      where p.DepartmentDepartmentId == 1 && (rp.DateTime.AddDays(2).Month/3) == 1 
      group new {rp, p} by new {rp.ProductProductId, p.ProductId, p.ProductName} 
      into grp 
      select new 
      { 
       grp.Key.ProductId, 
       grp.Key.ProductName, 
       PlannedAmount = grp.Sum(g => g.rp.Amount) 
      }; 

     Console.WriteLine("ProductId ProductName  PlannedAmount"); 
     foreach (var producAmount in amountByProducts) 
     { 
      Console.WriteLine("{0} \t\t{1} \t\t{2}", producAmount.ProductId, producAmount.ProductName, 
       producAmount.PlannedAmount); 
     } 
    } 
}