C# 查找Min DateTime并将DateTime分配给局部变量

C# 查找Min DateTime并将DateTime分配给局部变量,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我试图得到最小日期时间,然后是最大日期时间,并比较两者。但是,当我试图从LINQ中提取DateTime值时,会出现以下错误。任何帮助都将不胜感激 在此行引发错误:appChartDateStart=Convert.ToDateTime(dateStartType) 艾伦 无法将类型为“WhereSelectEnumerableTerator2[System.Linq.iGroup2[System.DateTime,LatencyApp.Domain.Models.ChartHist],f_uAn

我试图得到最小日期时间,然后是最大日期时间,并比较两者。但是,当我试图从LINQ中提取DateTime值时,会出现以下错误。任何帮助都将不胜感激

在此行引发错误:appChartDateStart=Convert.ToDateTime(dateStartType)

艾伦

无法将类型为“WhereSelectEnumerableTerator
2[System.Linq.iGroup
2[System.DateTime,LatencyApp.Domain.Models.ChartHist],f_uAnonymousType2`1[System.DateTime]]的对象强制转换为类型“System.IConvertible”

我的代码:

        DateTime appChartDateStart = DateTime.MinValue;
        DateTime appChartDateEnd = DateTime.MinValue;
        var dateStartType = from row in myRow
                                group row by row.LoginDateTime_Rounded into g
                                select new { MinDateTime = g.Min(row => row.LoginDateTime_Rounded) };
        var dateEndType = from row in myRow
                                group row by row.LoginDateTime_Rounded into g
                                select new { MinDateTime = g.Max(row => row.LoginDateTime_Rounded) };
        appChartDateStart = Convert.ToDateTime(dateStartType);
        appChartDateEnd = Convert.ToDateTime(dateEndType);
        TimeSpan difAppChart = appChartDateEnd - appChartDateStart;

这是因为您的select正在创建一个具有MinDateTime属性的匿名类型。Convert.TomDateTime方法不知道如何转换此匿名类型。试着做:

DateTime appChartDateStart = DateTime.MinValue;
DateTime appChartDateEnd = DateTime.MinValue;
var dateStartType = (from row in myRow
                        group row by row.LoginDateTime_Rounded into g
                        select new { MinDateTime = g.Min(row => row.LoginDateTime_Rounded) }).FirstOrDefault();
var dateEndType = (from row in myRow
                        group row by row.LoginDateTime_Rounded into g
                        select new { MinDateTime = g.Max(row => row.LoginDateTime_Rounded) }).FirstOrDefault();
appChartDateStart = Convert.ToDateTime(dateStartType);
appChartDateEnd = Convert.ToDateTime(dateEndType);
TimeSpan difAppChart = appChartDateEnd - appChartDateStart;

我在LINQ sintax上做的,效果很好,也比较干净:

DateTime appChartDateStart = DateTime.MinValue;
DateTime appChartDateEnd = DateTime.MinValue;

var dateStartType = myRow.GroupBy(x => x.LoginDateTime_Rounded).Select(x => x.Key).Min();
var dateEndType = myRow.GroupBy(x => x.LoginDateTime_Rounded).Select(x => x.Key).Max();

TimeSpan difAppChart = appChartDateEnd - appChartDateStart;
完成代码:

        DateTime appChartDateStart = DateTime.MinValue;
        DateTime appChartDateEnd = DateTime.MinValue;
        if (histByApp.Count() != 0)
        {
            var dateStartType = histByApp.GroupBy(x => x.LoginDateTime_Rounded).Select(x => x.Key).Min();
            var dateEndType = histByApp.GroupBy(x => x.LoginDateTime_Rounded).Select(x => x.Key).Max();
            appChartDateStart = Convert.ToDateTime(dateStartType);
            appChartDateEnd = Convert.ToDateTime(dateEndType);
        }
        TimeSpan difAppChart = appChartDateEnd - appChartDateStart;

谢谢你的回复。对,我忘了这是一个集合和单个对象。试试我修改过的代码。编辑你的答案,并将其包含在最后。