24

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'

2 Answers 2

82

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

}
0
0

sometimes addition of model on top of view is missing that time you will get same error eg. - @model List<Test.Admin.Models>

Not the answer you're looking for? Browse other questions tagged or ask your own question.