the*_*nth 15 c# linq linq-to-objects exception-handling
public readonly IEnumerable<string> PeriodToSelect = new string[] { "MONTH" };
var dataCollection = from p in somedata
from h in p.somemoredate
where h.Year > (DateTime.Now.Year - 2)
where PeriodToSelect.Contains(h.TimePeriod)
select new
{
p.Currency,
h.Year.Month, h.Value
};
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我为什么在下面的代码行中抛出异常?
int count = dataCollection.Count();
Run Code Online (Sandbox Code Playgroud)
这是例外:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ...
Run Code Online (Sandbox Code Playgroud)
Die*_*lli 14
当它试图执行谓词或预测时,这看起来像是linq2objects中的普通空引用异常.
如果"somedata"集合的某些元素为null,如果"h.Year"为null(这是什么类型?),或者如果"p.",那么你会得到一个空引用异常. somemoredate"是null ..
在 Count() 语句处抛出异常是因为 LINQ 使用延迟执行,并且实际的 LINQ 查询直到调用.Count()、.ToList()等才会执行。
| 归档时间: |
|
| 查看次数: |
37467 次 |
| 最近记录: |