linq查询匿名类型不能转换为POCO对象 [英] linq query anonymous type cannot be converted to POCO object
问题描述
我有以下linq查询...
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;
}
一个项目上可能有0或消息.我的目标是将UserProject对象的列表传递到我的MVC视图,每个UserProject对象可以具有消息的集合.我得到的错误如下
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
错误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>)
'具有一些无效的参数
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
当前message实体不具有Projects的导航属性……应该……以后再添加该更改……但此刻我只需要继续工作即可.
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.
现在,linq查询看起来像这样...
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>();
视图类如下所示
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 ...
错误1无法将类型'
System.Collections.Generic.IEnumerable<Riebro.Message>
'隐式转换为'System.Collections.Generic.List<Riebro.Message>
'.存在显式转换(您是否缺少演员表?)
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?)
推荐答案
您应在select语句中创建UserProject实例而不是匿名对象.
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
}
这篇关于linq查询匿名类型不能转换为POCO对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!