Thursday, October 8, 2009

Access to Modified Closure error

I was LINQ-ing today and had this obscure error that didn't have much meaning to until I trolled the tubes and came up with Franc Bouma's blog.

Franc's blog

I realized the error and would like to point out that LINQ doesn't like empty or null values in the WHERE clause. Be careful unless you like to get a generic LINQ error and go bonkers and waste countless hours scratching your head wondering what went wrong.


  245         private void DoNothing(List<SomeObject.SomeClass> someList1, List<SomeObject.RoomRateType> someList2)


  246         {


  247             string rphValue = String.Empty;


  248             List<SomeClass> someClassList = new List<SomeClass>();


  249             SomeClass someObjectVar = new SomeClass();


  250 


  251             foreach (SomeObject.SomeClass someObjectVar in someList1)


  252             {


  253                 if (someObjectVar.RPHSpecified)


  254                 {


  255                     rphValue = someObjectVar.RPH.ToString();


  256                     var linqQuery = from rrl in someList2


  257                                     where (rrl.RatePlanCode == rphValue) && (rrl.Rates[0].Base.AdditionalFeesExcludedIndicator == true)


  258                                     select rrl.RoomRateDescription.Name;


  259 


  260                     foreach (var linqItem in linqQuery)


  261                     {


  262                         someObjectVar.MoreDescription += linqItem;


  263                     }


  264 


  265                     someObjectVar.RphCode = someObjectVar.RPH;


  266                 }


  267             }


  268         }







the "rphValue" variable can be null or empty and although I know that what I am doing may not cause any trouble, I am sure in some more complex situation, the results will be dire

No comments:

Post a Comment