Anonymous LINQ request type cannot be converted to a POCO object

I have the following linq request ...

public List<UserProject> GetProjectsByUser(int userid) { //var query = return ( from p in this.Entities.Projects join c in this.Entities.Categories on p.CategoryId equals c.CategoryId join u in this.Entities.Users on p.UserId equals u.UserId where p.UserId == 11 select new { p.ProjectId, u.UserName, p.UserId, ProjectName = p.Name, ProjectDescription = p.Description, CategoryName = c.Name } into pcup join m in this.Entities.Messages on pcup.ProjectId equals m.ProjectId into pm select new { pcup.ProjectId, pcup.UserId, pcup.ProjectName, pcup.ProjectDescription, Messages = pm } ).ToList<UserProject>(); } 

and I have the following view object that I am trying to populate ....

 public class UserProject { UserProject() { Messages = new EnumerableQuery<Message>(); } public int ProjectId; public int UserId; public string ProjectName; public string ProjectDescription; public IEnumerable<Message> Messages; //public string UserName; //public string CategoryName; //public int CategoryId; } 

The project may have 0 or messages. My goal here is to pass a list of UserProjects into my MVC view, each UserProject can have a collection of messages. The error I am getting is as follows

Error 1 Instance argument: cannot convert from ' System.Linq.IQueryable<AnonymousType#1> ' to ' System.Collections.Generic.IEnumerable<Riebro.Services.UserProject> '

Error 2 ' System.Linq.IQueryable<AnonymousType#1> ' does not contain a definition for ' ToList ', and the best overload method is ' System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>) ' has some invalid arguments

Currently, the message object does not have navigation properties for projects ... it should ... I will add this change later ... but for now I just need to keep working.

EDIT

Currently, the linq query looks like this:

  return ( from p in this.Entities.Projects join c in this.Entities.Categories on p.CategoryId equals c.CategoryId join u in this.Entities.Users on p.UserId equals u.UserId where p.UserId == userid select new { p.ProjectId, u.UserName, p.UserId, p.Name, p.Description, //CategoryName = c.Name } into pcup join m in this.Entities.Messages on pcup.ProjectId equals m.ProjectId into pm select new UserProject { ProjectId = pcup.ProjectId, UserId = pcup.UserId, ProjectName = pcup.Name, ProjectDescription = pcup.Description, Messages = pm } ).ToList<UserProject>(); 

and the view class is as follows:

 public class UserProject { public UserProject() { Messages = new List<Message>(); } public int ProjectId; public string UserName; public int UserId; public string ProjectName; public string ProjectDescription; public List<Message> Messages; //public string CategoryName; //public int CategoryId; } 

and now I get the following error ...

Error 1: It is not possible to implicitly convert the type ' System.Collections.Generic.IEnumerable<Riebro.Message> ' to ' System.Collections.Generic.List<Riebro.Message> '. Explicit conversion exists (are you skipping listing?)

+6
source share
1 answer

You must create an instance of UserProject instead of an anonymous object in your select statement.

  select new UserProject { ProjectId = pcup.ProjectId, UserID = pcup.UserId, ProjectName = pcup.ProjectName, ProjectDescription = pcup.ProjectDescription, Messages = pm } 
+15
source

Source: https://habr.com/ru/post/908853/


All Articles