linq query anonymous type cannot be converted to POCO object

10,743

You should create a UserProject instance 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
            }
Share:
10,743
Kevin Donde
Author by

Kevin Donde

Updated on June 20, 2022

Comments

  • Kevin Donde
    Kevin Donde almost 2 years

    I have the following linq query ...

    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;
    }
    

    A project may have 0 or messages on it. My goal here is to pass a list of UserProject objects to my MVC view , each UserProject object can have a collection of messages. The error I get 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 extension method overload 'System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments

    Currently the messages entity does not have a navigation property to Projects ... it should ... I will add that change later ... but at the moment I just need to keep on working.

    EDIT

    As it stands now 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 looks like this ...

    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 I am now getting the following error ...

    Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Riebro.Message>' to 'System.Collections.Generic.List<Riebro.Message>'. An explicit conversion exists (are you missing a cast?)

  • Ahmad Mageed
    Ahmad Mageed about 12 years
    +1 in addition, the UserProject() constructor, which initializes Messages, should be made public, otherwise it will be inaccessible.
  • Kevin Donde
    Kevin Donde about 12 years
    Thanks ... that works initially ... but I am now stuck with ... Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Riebro.Message>' to 'System.Collections.Generic.List<Riebro.Message>'. An explicit conversion exists (are you missing a cast?)
  • Ufuk Hacıoğulları
    Ufuk Hacıoğulları about 12 years
    Use ToList() at the end of your query