Domanda

I have a ICollection of Projects in my user class

public ICollection<Project> Projects { get; set; }

When I try to render the count of projects in my view, it gives an error

<h2>You have @Model.Projects.Count() projects....</h2>

Any help appreciated.

È stato utile?

Soluzione

ICollection doesn't have a Count method, it has a Count property. You are probably getting confused with the LINQ Count extension method which is supported on an IEnumerable interface.

Just remove the parenthesis at the end of the Count call i.e.

<h2>You have @Model.Projects.Count projects...</h2>

Altri suggerimenti

I think I figured out what the issue was here. I added a constructor in my user class to handle the null reference exception

public User()
        {
            this.Roles = new List<Role>();
            this.Projects = new List<Project>();
        }

This did the trick. And ofcourse I called count without the paranthesis

In general, I've found it useful to write default constructors of every type, so that when stepping through a project like this I can visually see when each one is called. Not having a copy constructor or something similar can mask odd issues like this and make debugging infinitely frustrating.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top