I have this model
public class Project
{
[Required]
public Guid Id { get; set; }
[MaxLength(512)]
[Required]
public string ProjectName { get; set; }
public Category Category { get; set; }
[ForeignKey("Category")]
public Guid? CategoryId { get; set; }
}
public class Category
{
[Required]
public Guid Id { get; set; }
public string CategoryName { get; set; }
public ICollection<Project> Projects { get; set; }
}
and this is view model:
public class TipoProyectoViewModel
{
public string CategoryName { get; set; }
public IEnumerable<Project> MProject { get; set; }
}
When I am writing my code to get a list of projects group by category
Category = House
{
{ Id = 1, ProjectName = "my project" }
{ Id = 2, ProjectName = "my project2" }
}
Category = Cars
{
{ Id = 3, ProjectName = "my project3" }
{ Id = 5, ProjectName = "my project5" }
}
This is what I have in my controller:
var categories = _dbContext.Category
.Include(c => c.Projects)
.GroupBy(e => e.CategoryName)
.Select(e => new TipoProyectoViewModel()
{
CategoryName = e.Key,
MProject = e.Key.Projects
});
My problem is that I am getting an error in my select when I write
MProject = e.Key.Projects
String does not contain a definition for Projects and no extension method "projects"accepting a first argument of type string could be found. Are you missing a reference?
what am I doing wrong?
You're accessing e.Key
, which is a string
key of a grouping. Apparently, it doesn't have a property or a method .Projects
.
You can collect the Projects
from your grouped items like this
MProject = e.SelectMany(cat => cat.Projects);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.