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

时间:2012-02-19 21:25:49

标签: linq entity-framework linq-to-sql

我有以下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>”。存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分:15)

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

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