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.
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?)
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With