代码之家  ›  专栏  ›  技术社区  ›  sgmoore

使用LinqToSql查找每个产品的最新价格

  •  0
  • sgmoore  · 技术社区  · 16 年前

    日期

    我想知道某个日期的最新价格。在Sql中,我可以做类似的事情

    ` 从中选择*(

    选择ROW_NUMBER()OVER(按产品ID排序按日期描述)作为ROW_NUMBER,

    )tempTable

    其中row_number=1 `

    3 回复  |  直到 16 年前
        1
  •  1
  •   Jon Skeet    16 年前

    var query = from product in db.Products
                where product.Date < targetDate
                orderby product.Date descending
                group product by product.ProductID into groups
                select groups.First();
    

    有可能打电话 First FirstOrDefault

        2
  •  0
  •   David Espart    16 年前
     var result = (
                   from context.table where date < targetDate 
                   and ProductID = targetproduct 
                   orderby date descending
                   select new {ProductID, price, Date}
                  ).Take(1);
    

        3
  •  0
  •   bruno conde    16 年前

    我认为这会完成这项工作。..

    var qry = ProductPrices.Where(pp => pp.Date < targetDate)
                           .Max(pp => pp.Date.Ticks);