locked
I am having issues with Count() RRS feed

  • Question

  • User895691971 posted

    Hi, I am having slight issues with the Count(). What I want to get is something like a count of the data being sent to me.

    But the error I am getting is something like: 

     Non-invocable member 'System.Collections.ObjectModel.ReadOnlyCollection<object>.Count' cannot be used like a method.

    What I want to get is something like 8 or 10 or how many the rows are matching. The query I am having is: 

    var totalLikes = db2.Query("SELECT * FROM LikesOnPosts WHERE OnPost =@0", row.PostId);

    the row.PostId suggests that there is another query that creates this result.

    Why I am not able to use it?

    However, I read a post of Mike about this too; sharing his views about IEnumerable Enumberable. What are these? Can I know about them too?

    Monday, August 26, 2013 12:10 PM

Answers

  • User281315223 posted

    Since you are using the Query method to populate your totalLikes variable : 

    var totalLikes = db2.Query("SELECT * FROM LikesOnPosts WHERE OnPost =@0", row.PostId);

    The Query method should be returning an IEnumerable<Object> collection, which should support the Count() method, however within your code it is being used both as a method and as property : 

    <div class="likes">@if(totalLikes.Count() != 0) {@totalLikes.Count<text> people like this.</text>}</div>

    So you might want to change both of those instances of Count() to use either the method : 

    <div class="likes">@if(totalLikes.Count() != 0) {@totalLikes.Count()<text> people like this.</text>}</div>

    or the property : 

    <div class="likes">@if(totalLikes.Count != 0) {@totalLikes.Count<text> people like this.</text>}</div>

    You could even use the Any() method as well : 

    @if(totalLikes.Any())
    {
         <div class="likes">
                @totalLikes.Count
                <text> people like this.</text>
         </div>
    } 

    (Whichever seems to work in your situation)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2013 1:03 PM
  • User-821857111 posted

    can I get a tutorial for IEnumberable and Enumerable? I don't know what they are, so maybe I am falling in errors because of this.

    See if this helps: http://www.mikesdotnetting.com/Article/214/How-To-Check-If-A-Query-Returns-Data-In-ASP.NET-Web-Pages

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2013 4:51 PM
  • User-821857111 posted

    Enumerable is a static class that provides a set of methods for querying objects that implement the IEnumerable<T> interface. You cannot instantiate an instance of Enumerable. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 27, 2013 4:25 AM

All replies

  • User281315223 posted

    Depending on the context, Count can either be used as a method or as a property, so you often have to use it accordingly based on your context : 

    //Example as a Property
    YourCollection.Count;
    
    //Example as a Method
    YourCollection.Count();
    
    //Example using Length (sometimes available)
    YourCollection.Length;

    In your case, you are working with a ReadOnlyCollection, which based on its documentation does not have a method called "Count". It does however have a Count property which you should be able to use exactly the same way (only leave off the parentheses) : 

    YourReadOnlyCollection.Count;

    Regarding IEnumerables

    The IEnumerable Interface is one that is inherited by many different types of collections within .NET such as Lists. IEnumerables are useful in that they provide an easy mechanism (an interator) to actually iterate through the collections. Instances of the IEnumerable class (Enumerables) are also one of the base classes used with LINQ which exposes a multitude of different methods that allow you to easily query your collections as well.

    Monday, August 26, 2013 12:14 PM
  • User895691971 posted

    The error shows up when I use it as Property or Method. I have not used length.

    Monday, August 26, 2013 12:19 PM
  • User281315223 posted

    Could you post the code where you are actually calling either the method or the property (and any additional code that surrounds it may be useful). I'm currently just going off th error message that was provided.

    Monday, August 26, 2013 12:23 PM
  • User895691971 posted

    The related code to this post is: 

     @{
                                var totalLikes = db2.Query("SELECT * FROM LikesOnPosts WHERE OnPost =@0", row.PostId);
                                var myLike = db2.Query("SELECT * FROM LikesOnPosts WHERE OnPost =@0 AND ById =@1", row.PostId, WebSecurity.CurrentUserId);
                                var selectComments = db2.Query("SELECT * FROM CommentOnPosts WHERE OnPost =@0", row.PostId);
                            }
                        <div class='my-views-on-post_@row.PostId'>
                        <button onclick="toggleLike(@row.PostId)">Like</button><input id="comment-box" type="text" placeholder="Write some words..." />
                        </div>
                        <div class="total-activity">
                            <div class="likes">@if(totalLikes.Count() != 0) {@totalLikes.Count<text> people like this.</text>}</div>
                        <textarea placeholder="Write some words"></textarea>
                        </div>

    And the row.PostId comes from:

    @foreach (var row in result2) {

    I have not posted the code from this foreach as it would be lengthy.

    Monday, August 26, 2013 12:26 PM
  • User1713851783 posted

    Maybe this thread could help: Count' cannot be used like a method Error.

    Monday, August 26, 2013 12:43 PM
  • User895691971 posted

    The post of Mike I mentioned; it was from this thread! So its not helpfull. I have been through this. And tried what Mike said.

    Monday, August 26, 2013 12:48 PM
  • User753101303 posted

    In your ealier post you used Count two times one time with Count() and another one with Count. Are you 100% you test Mike suggestion by using Count everywhere it appears.

    Currently it seems you have :

    totalLikes.Count() != 0) {@totalLikes.Count etc...

    Monday, August 26, 2013 12:55 PM
  • User895691971 posted

    I am shifting from Count to Count() for the code inside the block. Not the condition!

    Monday, August 26, 2013 12:58 PM
  • User281315223 posted

    Since you are using the Query method to populate your totalLikes variable : 

    var totalLikes = db2.Query("SELECT * FROM LikesOnPosts WHERE OnPost =@0", row.PostId);

    The Query method should be returning an IEnumerable<Object> collection, which should support the Count() method, however within your code it is being used both as a method and as property : 

    <div class="likes">@if(totalLikes.Count() != 0) {@totalLikes.Count<text> people like this.</text>}</div>

    So you might want to change both of those instances of Count() to use either the method : 

    <div class="likes">@if(totalLikes.Count() != 0) {@totalLikes.Count()<text> people like this.</text>}</div>

    or the property : 

    <div class="likes">@if(totalLikes.Count != 0) {@totalLikes.Count<text> people like this.</text>}</div>

    You could even use the Any() method as well : 

    @if(totalLikes.Any())
    {
         <div class="likes">
                @totalLikes.Count
                <text> people like this.</text>
         </div>
    } 

    (Whichever seems to work in your situation)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2013 1:03 PM
  • User895691971 posted

    The property thing worked!

    Hey Rion, can I get a tutorial for IEnumberable and Enumerable? I don't know what they are, so maybe I am falling in errors because of this. 

    Monday, August 26, 2013 1:08 PM
  • User753101303 posted

    Not sure if you meant you realized this is the issue or if you think this is not needed. Wether or not this is a condition this is the exact same thing and it has to be called the same way...

    Monday, August 26, 2013 4:26 PM
  • User-821857111 posted

    can I get a tutorial for IEnumberable and Enumerable? I don't know what they are, so maybe I am falling in errors because of this.

    See if this helps: http://www.mikesdotnetting.com/Article/214/How-To-Check-If-A-Query-Returns-Data-In-ASP.NET-Web-Pages

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2013 4:51 PM
  • User895691971 posted

    Afzaal.Ahmad.Zeeshan

    can I get a tutorial for IEnumberable and Enumerable? I don't know what they are, so maybe I am falling in errors because of this.

    See if this helps: http://www.mikesdotnetting.com/Article/214/How-To-Check-If-A-Query-Returns-Data-In-ASP.NET-Web-Pages

    Thanks for the link Mike. 

    From the link all that I concluded was something like: IEnumberable are values which have no nulls while Enumberables do have null. And we need to use

    if(value_result != null) {
     // code
    }

    To make sure no execption is thrown.

    Secondly the <dynamic> values mostly use properties i.e. Count; instead of methods i.e. Count().

    Third, the conversion tutorial was outstanding, teaching how to prevent the data from being changed into dynamic type.

    Tuesday, August 27, 2013 1:46 AM
  • User-821857111 posted

    Enumerable is a static class that provides a set of methods for querying objects that implement the IEnumerable<T> interface. You cannot instantiate an instance of Enumerable. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 27, 2013 4:25 AM