ReSharper 5 does not propose to convert to LINQ
I have this code:
41 private void GetMagazinesByTableTypeCompleted(object sender, GetMagazinesByTableTypeCompletedEventArgs e)
42 {
43 if (e.Error == null)
44 {
45 foreach (var magazine in e.Result)
46 {
47 this.magazines.Add(new MagazinePE(magazine));
48 }
49 this.magazinesListBox.ItemsSource = this.magazines;
50 }
51 else
52 {
53 Tools.ShowErrorMessage(e.Error);
54 }
55 Proxy.GetWCFServiceProxy().GetMagazinesByTableTypeCompleted -= this.GetMagazinesByTableTypeCompleted;
56 }
The foreach in line 45 can be rewritten as follows:
39 private void GetMagazinesByTableTypeCompleted(object sender, GetMagazinesByTableTypeCompletedEventArgs e)
40 {
41 if (e.Error == null)
42 {
43 this.magazinesListBox.ItemsSource = e.Result.Select(p => new MagazinePE(p)).ToList();
44 }
45 else
46 {
47 Tools.ShowErrorMessage(e.Error);
48 }
49 Proxy.GetWCFServiceProxy().GetMagazinesByTableTypeCompleted -= this.GetMagazinesByTableTypeCompleted;
50 }
So far R# only proposes to replace the foreach with for.
Please sign in to leave a comment.
Hello Gabriel Lozano-Moran,
No, it cannot be rewritten as such, there is no information about the field
"magazines" and if it is empty before calling this method or not. If that
list is not empty, the behaviour would be quite different. If you use local
variable instead of field, I think ReSharper will happily suggest to replace
it with LINQ.
Sincerely,
Ilya Ryzhenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
G> I have this code:
G>
G> 41 private void GetMagazinesByTableTypeCompleted(object sender,
G> GetMagazinesByTableTypeCompletedEventArgs e)
G> 42 {
G> 43 if (e.Error == null)
G> 44 {
G> 45 foreach (var magazine in e.Result)
G> 46 {
G> 47 this.magazines.Add(new MagazinePE(magazine));
G> 48 }
G> 49 this.magazinesListBox.ItemsSource = this.magazines;
G> 50 }
G> 51 else
G> 52 {
G> 53 Tools.ShowErrorMessage(e.Error);
G> 54 }
G> 55 Proxy.GetWCFServiceProxy().GetMagazinesByTableTypeCompleted
G> -= this.GetMagazinesByTableTypeCompleted;
G> 56 }
G> The foreach in line 45 can be rewritten as follows:
G>
G> 39 private void GetMagazinesByTableTypeCompleted(object
G> sender, GetMagazinesByTableTypeCompletedEventArgs e)
G> 40 {
G> 41 if (e.Error == null)
G> 42 {
G> 43 this.magazinesListBox.ItemsSource =
G> e.Result.Select(p => new MagazinePE(p)).ToList();
G> 44 }
G> 45 else
G> 46 {
G> 47 Tools.ShowErrorMessage(e.Error);
G> 48 }
G> 49
G> Proxy.GetWCFServiceProxy().GetMagazinesByTableTypeCompleted -=
G> this.GetMagazinesByTableTypeCompleted;
G> 50 }
G> So far R# only proposes to replace the foreach with for.
G>
G> ---
G> Original message URL:
G> http://www.jetbrains.net/devnet/message/5247622#5247622
Hello Gabriel Lozano-Moran,
It could suggest replacing with AddRange, though. I assume here "magazines"
is of type List http://www.jetbrains.net/devnet/message/5247622#5247622
Sorry my bad, you are right. I refactored more than just that block which off course Resharper could not know.
It is actually an ObservableCollection of MagazineBE. AddRange() cannot be used as I need to convert from MagazineBE to MagazinePE (I know, it is confusing with all these magazine types but I can't help it :-))