2

我在 SQL 服务器中有我需要的查询,但我使用的是实体框架,并且希望使用 LINQ 获得相同的结果。这是SQL:

select convert(varchar, date_recorded, 12), MAX(outdoor_temp), MAX(wind_speed)
from weatherdata
group by convert(varchar, date_recorded, 12)
order by convert(varchar, date_recorded, 12) DESC ;

这会将我的日期时间列转换为允许我正确分组的正确格式。基本上我需要能够将我的 SQL 日期时间转换为 yymmdd 格式。

非常感谢

4

1 回答 1

0
class Program
    {
        static void Main(string[] args)
        {

            var entries = new List<Entry>()
                {
                    {new Entry { Date = DateTime.UtcNow, Outdoor_Temp = 10, Wind_Speed = 5 }},
                    {new Entry { Date = DateTime.UtcNow, Outdoor_Temp = 5, Wind_Speed = 10}},
                    {new Entry { Date = DateTime.UtcNow.AddDays(-1), Outdoor_Temp = 15, Wind_Speed = 7}}
                };

            (from e in entries 
             group e by e.Date.ToShortDateString() into g
             select 
                new 
                {
                    StringDate = g.Key,
                    MaxWind_Speed = g.Max(entry => entry.Wind_Speed),
                    MaxOutdoor_Temp = g.Max(entry => entry.Outdoor_Temp)
                }
             ).ToList().ForEach(Console.WriteLine);

        }

        public class Entry
        {
            public DateTime Date { get; set; }
            public int Outdoor_Temp { get; set; }
            public int Wind_Speed { get; set; }
            public override string ToString()
            {
                return string.Format("Date : {0}, Outdoor_Temp : {1}, Wind_Speed : {2}", Date, Outdoor_Temp, Wind_Speed);
            }
        }
    }
于 2012-07-25T09:38:47.663 回答