【问题标题】:LINQ Operator '==' cannot be applied to operands of type 'method group' and 'int'LINQ 运算符“==”不能应用于“方法组”和“int”类型的操作数
【发布时间】:2012-04-17 21:48:00
【问题描述】:

我有类似以下的内容:

    var lst = db.usp_GetLst(ID,Name, Type);

    if (lst.Count == 0)
    {

    }

我在 lst.Count == 0 下得到一个大谎言,它说:

运算符“==”不能应用于“方法组”和“int”类型的操作数

【问题讨论】:

    标签: linq


    【解决方案1】:

    Enumerable.Count 是扩展方法,而不是属性。这意味着 usp_GetLst 可能会返回 IEnumerable<T>(或其他等价物),而不是您预期的 IList<T>ICollection<T> 的派生词。

    // Notice we use lst.Count() instead of lst.Count
    if (lst.Count() == 0)
    {
    
    }
    
    // However lst.Count() may walk the entire enumeration, depending on its
    // implementation. Instead favor Any() when testing for the presence
    // or absence of members in some enumeration.
    if (!lst.Any())
    {
    
    }
    

    【讨论】:

      【解决方案2】:

      有时在视图顶部添加模型时会丢失相同的错误 例如。 - @model 列表

      【讨论】:

        猜你喜欢
        • 2013-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-25
        • 2011-12-31
        • 2022-01-07
        相关资源
        最近更新 更多