LINQ查找具有相应时间字段的最大/最小值



我有一个气象站的数据记录表,我从中查询结果到WPF ListBox

表格结构为:

  • 日期
  • 时间
  • 温度
  • 雨天
  • 湿度
  • 等等

我有一个运行良好的查询:

var q = from c in db.Apr11log
                group c by c.LogDate into g
                orderby g.Key
                select new
                {
                    LogDate = g.Key,
                    MaxTemp = g.Max(c => c.Temp),
                    MinTemp = g.Min(c => c.Temp),                                             
                    Rain = g.Max(c => c.Rain_today),
                };      

然而,我正在尝试获得最高温度和最低温度的相应时间,即

TimeMax = .....
TimeMin = .....

我在谷歌上搜索了又搜索,但没有发现任何有用的东西。

var q = from c in db.Apr11log
        group c by c.LogDate into g
        orderby g.Key
        select new
        {
            LogDate = g.Key,
            MaxRow = g.OrderByDescending(c => c.Temp).Select(c => new { c.LogDate, c.Temp }).First(),
            MinRow = g.OrderBy(c => c.Temp).Select(c => new { c.LogDate, c.Temp }).First(),
            Rain = g.Max(c => c.Rain_today),
        };

var q = from c in db.Apr11log
        group c by c.LogDate into g
        orderby g.Key
        let maxRow = g.OrderByDescending(c => c.Temp).First()
        let minRow = g.OrderBy(c => c.Temp).First()
        select new
        {
            LogDate = g.Key,
            MaxTemp = maxRow.Temp,
            MaxTempDate = maxRow.LogDate,
            MinTemp = minRow.Temp,
            MinTempDate = minRow.LogDate,
            Rain = g.Max(c => c.Rain_today),
        };
let maxTemp = c.Max(c=>c.Temp)
let minTemp = c.Min(c=>c.Temp)
select new {
    LogDate = g.Key,
    MaxTemp = maxTemp,
    MinTemp = minTemp,
    MaxTime = g.FirstOrDefault(c=>c.Temp = maxTemp).Time,
    MinTime = g.FirstOrDefault(c => c.Temp = minTemp).Time,
    Rain = g.Max(c => c.Rain_today),
       };      

相关内容

  • 没有找到相关文章

最新更新