我通常在Linq Pad中编写测试代码,然后将其添加到我的MVC应用程序中。以下代码适用于Linq Pad,但Visual Studio会抛出空引用异常。
var first =
(from a in AdviceLineCalls
where a.CallDate != null
orderby a.CallDate
select a.CallDate).First();
var last =
(from a in AdviceLineCalls
where a.CallDate != null
orderby a.CallDate descending
select a.CallDate).First();
var calls =
(from a in AdviceLineCalls
join s in Staff on a.StaffID equals s.StaffID
where a.CallLength != null
&& s.Employed == true
select new Call()
{
Staff = s.LastName,
Minutes = a.CallLength.Value,
CallDate = a.CallDate
}).ToList();
var whoCalled =
from c in calls
where (c.CallDate >= first && c.CallDate <= last)
group c by c.Staff into grp
let count = grp.Count()
let mins = grp.Sum (g => g.Minutes)
let avg = grp.Average (g => g.Minutes)
select new WhoCalled()
{Status = grp.Key, Frequency = count, Minutes = mins, Avg = avg};
在Visual Studio中,我在who.called on c.CallDate上的where子句中得到一个空引用错误:对象引用未设置为对象的实例。我不确定为什么这可以在Linq Pad中工作,但不能在Visual Studio中工作。