-1

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.Linq.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

Femaref
  • 58,195
  • 7
  • 126
  • 170
paparazzo
  • 42,665
  • 20
  • 93
  • 158
  • What is `FieldDefs`? Could you post the code for the `DispLevel` property? – Femaref Mar 18 '11 at 23:36
  • FieldDefs is a FieldDef DispLevel is just a simply Byte property of FieldDef public byte DispOrder { get { return dispOrder; } set { dispOrder = value; } } – paparazzo Mar 19 '11 at 13:50

1 Answers1

0

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.

Mike
  • 968
  • 11
  • 13