LINQ运算符'=='不能应用于'method group'和'int'类型的操作数(LINQ Operator '=='

编程入门 行业动态 更新时间:2024-10-11 23:22:17
LINQ运算符'=='不能应用于'method group'和'int'类型的操作数(LINQ Operator '==' cannot be applied to operands of type 'method group' and 'int')

我有以下内容:

var lst = db.usp_GetLst(ID,Name, Type); if (lst.Count == 0) { }

我在lst.Count == 0下得到了一个狡猾的谎言,它说:

运算符'=='不能应用于'method group'和'int'类型的操作数

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'

最满意答案

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

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

更多推荐

本文发布于:2023-07-31 08:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用于   运算符   类型   操作   group

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!