Quantcast
Channel: YouTrackReSharper (RSRP) - Bug and Issue Tracker
Viewing all articles
Browse latest Browse all 106942

RSRP-288335: 'Convert part of body to Linq' could do better in this case

$
0
0
Reporter Drew Noakes (drewnoakes) Drew Noakes (drewnoakes)
Created Feb 16, 2012 12:22:32 AM
Updated Feb 16, 2012 12:22:32 AM
Priority Normal
Type Feature
Fix versions No Fix versions
State Submitted
Assignee Unassigned
Subsystem Code Insight
Affected versions 6.1
Fixed in build No Fixed in build
foreach (var item in _activeBehaviors)
{
    var elements = VisualTreeHelper.FindElementsInHostCoordinates(point.Position, Application.Current.RootVisual);

    if (elements.Contains(item.AssociatedObject))
    {
        _activeBehavior = item;
        _activeBehavior.OnCapture();
        break;
    }
}


...becomes...

foreach (var item in from item in _activeBehaviors
                     let elements = VisualTreeHelper.FindElementsInHostCoordinates(point.Position, Application.Current.RootVisual)
                     where elements.Contains(item.AssociatedObject)
                     select item)
{
    _activeBehavior = item;
    _activeBehavior.OnCapture();
    break;
}


But, given that 'item' is of a reference type, this code could be simplified to:

var first = (from item in _activeBehaviors
            let elements = VisualTreeHelper.FindElementsInHostCoordinates(point.Position, Application.Current.RootVisual)
            where elements.Contains(item.AssociatedObject)
            select item).FirstOrDefault();
if (first != null)
{
    _activeBehavior = first;
    _activeBehavior.OnCapture();
}


In this case, the code can actually be refactored even further into this:

var elements = VisualTreeHelper.FindElementsInHostCoordinates(point.Position, Application.Current.RootVisual);
var first = _activeBehaviors.FirstOrDefault(item => elements.Contains(item.AssociatedObject));
if (first != null)
{
    _activeBehavior = first;
    _activeBehavior.OnCapture();
}


But I'm not sure whether there's enough information available about side effects for R# to make this last transformation.

Viewing all articles
Browse latest Browse all 106942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>