linq查询匿名类型无法转换为POCO对象



我有以下linq查询。。。

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

我有下面的视图对象,我正在尝试填充。。。。

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

一个项目上可能有0或消息。我的目标是将UserProject对象的列表传递给我的MVC视图,每个UserProject对象都可以有一个消息集合。我得到的错误如下

错误1实例参数:无法从"System.Linq.IQueryable<AnonymousType#1>"转换为"System.Collections.Generic.IEnumerable<Riebro.Services.UserProject>"

错误2"System.Linq.IQueryable<AnonymousType#1>"不包含"ToList"的定义,并且最佳扩展方法重载"System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>)"具有一些无效参数

当前消息实体没有指向项目的导航属性。。。它应该。。。我稍后会添加此更改。。。但现在我只需要继续工作。

编辑

目前的linq查询如下。。。

    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>();

视图类看起来是这样的。。。

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

我现在得到以下错误。。。

错误1无法将类型"System.Collections.Generic.IEnumerable<Riebro.Message>"隐式转换为"System.Collections.Generic.List<Riebro.Message>"。存在显式转换(是否缺少强制转换?)

您应该在select语句中创建一个UserProject实例,而不是一个匿名对象。

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

最新更新