C# 字符串引用未设置为字符串的实例。参数名称:输入ASP.Net MVC
为什么我看到这个错误prnt.sc/11btyns。只有当我将大多数时间从星期一改为最后一周的星期二,将周末改为最后一周的星期日时,才会发生这种情况C# 字符串引用未设置为字符串的实例。参数名称:输入ASP.Net MVC,c#,asp.net-mvc,C#,Asp.net Mvc,为什么我看到这个错误prnt.sc/11btyns。只有当我将大多数时间从星期一改为最后一周的星期二,将周末改为最后一周的星期日时,才会发生这种情况 var mostRecentMonday = DateTime.Now.AddDays(-7).StartOfWeek(DayOfWeek.Monday); var weekEnd = mostRecentMonday.AddDays(7).AddSeconds(-1); var lastWeekMonday = mostRecentMonda
var mostRecentMonday = DateTime.Now.AddDays(-7).StartOfWeek(DayOfWeek.Monday);
var weekEnd = mostRecentMonday.AddDays(7).AddSeconds(-1);
var lastWeekMonday = mostRecentMonday.AddDays(-7).StartOfWeek(DayOfWeek.Monday);
var lastWeekSunday = lastWeekMonday.AddDays(7).AddSeconds(-1);
模范班
public DateTime? FeedbackDateTime { get; set; }
public DateTime? FeedbackSharedDateTime { get; set; }
public string AuditorAHT { get; set; }
ReportVM将数据分组并显示在视图中
public string FeedbackSharedBy { get; set; }
public int AuditCount { get; set; }
public string AudtAht { get; set; }
将审计员执行的操作保存为中的持续时间的控制器
public string AuditorAHT { get; set; }
下面的操作应显示审计员姓名、计数和平均花费时间
var audtName = db.Chats.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) >= mostRecentMonday
&& System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) <= weekEnd && x.Feedback != null && x.FeedbackSharedBy != null).Select(x => new {
x.FeedbackSharedBy,
x.AuditorAHT
}).ToList() // this hits the database
// We need to do grouping in the code (rather than the db)
// because timespans are stored as strings
.GroupBy(e => e.FeedbackSharedBy)
.Select(g => new ReportVM
{
FeedbackSharedBy = g.Key,
AuditCount = g.Count(),
AudtAht = TimeSpan.FromSeconds(g.Sum(t => TimeSpan.Parse(t.AuditorAHT).TotalSeconds / g.Count())).ToString()
})
.OrderByDescending(s => s.AuditCount).ToList();
ViewBag.AudtReport = audtName;
请建议只需进行两次更改即可解决此问题
谢谢大家!
StartOfWeek()
从哪里来?日期时间EXNTE,上面的代码用Extn更新。请查看该代码中的任何地方都没有字符串,您需要向我们显示具有字符串的代码(或字符串解析)。@PeterB在我的问题中查看更新的代码。为什么要使用AddSeconds(),请删除它并进行测试。
var audtName = db.Chats.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) >= mostRecentMonday
&& System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) <= weekEnd && x.Feedback != null && x.FeedbackSharedBy != null).Select(x => new {
x.FeedbackSharedBy,
x.AuditorAHT
}).ToList() // this hits the database
// We need to do grouping in the code (rather than the db)
// because timespans are stored as strings
.GroupBy(e => e.FeedbackSharedBy)
.Select(g => new ReportVM
{
FeedbackSharedBy = g.Key,
AuditCount = g.Count(),
AudtAht = TimeSpan.FromSeconds(g.Sum(t => TimeSpan.Parse(t.AuditorAHT).TotalSeconds / g.Count())).ToString()
})
.OrderByDescending(s => s.AuditCount).ToList();
ViewBag.AudtReport = audtName;
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
return dt.AddDays(-1 * diff).Date;
}
static GregorianCalendar _gc = new GregorianCalendar();
public static int GetWeekOfMonth(this DateTime time)
{
DateTime first = new DateTime(time.Year, time.Month, 1);
return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
}
static int GetWeekOfYear(this DateTime time)
{
return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
}
static DateTime ToCleanDateTime(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
}
public static DateTime ToCleanDateTime(this DateTime? dt)
{
if (dt.HasValue)
{
return dt.Value.ToCleanDateTime();
}
return DateTime.Now; // if dt doesn't have value, return current DateTime.
}
}
Update Chats
Set AuditorAHT = '00:00:00'
Where AuditorAHT IS NULL;