3

I have written something like this below, Resharper says Local variable refKey is never used. How Can I make this written a little nicer?

 var temp = this.SomeCollection.ToList(); foreach (var refKey in temp.Where(refKey => this.Teachers.License_key == refKey.ReferenceKey)) { someBool = true; this.NotifyPropertyChanged("SomeProperty"); } 
9
  • 1
    Define Nicer - performance / Elegance / length of lines? Incidentally this wont compile as You use the same variable name "refKey" as the variable and the lambda variable Commented Feb 20, 2014 at 15:03
  • You are using the same variable name in the loop variable and the lambda parameter. Commented Feb 20, 2014 at 15:05
  • Resharper is telling you that you are making a loop with refkey var, and if you see you ar enever using it! so what is the point! Commented Feb 20, 2014 at 15:05
  • 1
    When your code is working and you only need to improve it, please post the question on Code Review Commented Feb 20, 2014 at 15:08
  • 1
    Suppose there are fifty keys that match that predicate; do you really want NotifyPropertyChanged called fifty times? Commented Feb 20, 2014 at 16:11

1 Answer 1

9

It appears that what you want to do is execute some code if there are any items in that query, rather than executing those lines of code for every item in the query. The Any method allows you to do this more effectively:

if(temp.Any(refKey => this.Teachers.License_key == refKey.ReferenceKey)) { someBool = true; this.NotifyPropertyChanged("SomeProperty"); } 
Sign up to request clarification or add additional context in comments.

Comments