LINQ Operator '==' cannot be applied to op

2020-07-02 12:00发布

I have something like the following:

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

    if (lst.Count == 0)
    {

    }

I get a swigly lie under lst.Count == 0 and it says:

Operator '==' cannot be applied to operands of type 'method group' and 'int'

标签: linq
1条回答
家丑人穷心不美
2楼-- · 2020-07-02 12:14

Enumerable.Count is an extension method, not a property. This means usp_GetLst probably returns IEnumerable<T> (or some equivalent) rather than a derivative of IList<T> or ICollection<T> which you expected.

// 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())
{

}
查看更多
登录 后发表回答