Resharper警告代码启发式无法访问

时间:2016-06-20 09:45:17

标签: c# resharper

我正在使用以下代码,并想知道为什么其他部分根据resharper无法访问。

private bool SomeMEthod(some parameter)
{
    bool status = false; 
    var someCollection = _entity.CustomerPaymentStatus.Where(record => record.CustomerPaymentId == paymentId && record.CurrentRecord == true);
    if (someCollection != null)
    {
        var receivedPayment = someCollection.FirstOrDefault();
        /*some code to save data into DB*/
        status = true;
    }
    else
    {
        //Some code here to log failure scenario

        //here Resharper giving me warning
        //code is heuristically unreachable
    }
    return status;
}

我检查了几个帖子,但不清楚Code is heuristically unreachable

请任何想法。

3 个答案:

答案 0 :(得分:5)

.Where()永远不会返回null,但总是返回IEnumerable<T>(或IQueryable<T>。枚举可能有0个项目,但它仍然是非null的可枚举。

答案 1 :(得分:2)

如果找不到匹配的记录,Linq Where查询将返回空的IEnumerable,因此someCollectionnull非常不明智 - 尽管ReSharper确实如此似乎没有完全确定它。

另见MSDN Where

答案 2 :(得分:0)

假设_entity是DbContext,那么Where总是返回一个IQueryable对象。

 IQueryable<T> someCollection = _entity.CustomerPaymentStatus.Where(...);

IQueryable对象永远不会为null(否则你将无法查询它)。

测试该对象的null doe不执行查询。所以

 if (someCollection != null)

相同
 if (true)