开发者

LINQ .NET 4.0 - WHERE alone OK - ORDERBY alone OK - WHERE and ORDERBY Fails

Below runs correctly without error:

IEnumerable<FieldDef> FieldDefQuery =
    from fds in FieldDefs 
    where fds.DispLevel == enumDispLevel.Grid     
    select fds;

Below runs correctly without error:

IEnumerable<FieldDef> FieldDefQuery =
    from fds in FieldDefs    
    orderby fds.DispOrder ascending 
    select fds;

Below fails:

IEnumerable<FieldDef> FieldDefQuery =
    from fds in FieldDefs 
    where fds.DispLevel == enumDispLevel.Grid    
    orderby fds.DispOrder ascending 
    select fds;
foreach (FieldDef fd in FieldDefQuery)
{
    Debug.WriteLine(fd.DispName);
}

With both where and orderby clauses it fails at execution time with the following message:

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred
Parameter name: Parameter index is out of range.
   at Gabe2a.GabeLib.FieldDef.get_DispLevel() in 
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.L开发者_如何学JAVAinq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() 

I just cannot figure out how/why where-clause alone is fine and orderby-clause alone is fine but the two clauses together fail


I believe the issue is that LINQ queries are not evaluated until you enumerate through them. DispLevel could straight out throw new Exception() and the first two blocks, on their own, would cause no problems. When you get to the third block where you enumerate the query the exception gets thrown.

Look deeper into the DispLevel property and you'll find the cause of the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜