Tôi có truy vấn LINQ sau ...truy vấn LINQ loại vô danh không thể được chuyển đổi sang đối tượng POCO
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>();
}
và tôi có các đối tượng xem sau mà tôi đang cố gắng để cư ....
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;
}
Dự án có thể có 0 hoặc tin nhắn trên đó. Mục tiêu của tôi ở đây là chuyển một danh sách các đối tượng UserProject đến khung nhìn MVC của tôi, mỗi đối tượng UserProject có thể có một tập hợp các thông điệp. Các lỗi tôi nhận được là như sau
Lỗi lập luận 1 Instance: không thể chuyển đổi từ '
System.Linq.IQueryable<AnonymousType#1>
' thành 'System.Collections.Generic.IEnumerable<Riebro.Services.UserProject>
'Lỗi 2 '
System.Linq.IQueryable<AnonymousType#1>
' không chứa một định nghĩa cho 'ToList
' và phần mở rộng tốt nhất phương pháp quá tải 'System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>)
' có một số đối số không hợp lệ
Hiện tại thực thể thư không có thuộc tính điều hướng cho Dự án ... nên ... Tôi sẽ thêm thay đổi đó sau ... nhưng tại thời điểm tôi chỉ cần phải ke ep làm việc.
EDIT
Khi đứng tại các truy vấn LINQ trông như thế này ...
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>();
và lớp nhìn trông như thế này ...
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;
}
và Tôi bây giờ gặp lỗi sau ...
Lỗi 1 C chú thích chuyển đổi hoàn toàn loại '
System.Collections.Generic.IEnumerable<Riebro.Message>
' thành 'System.Collections.Generic.List<Riebro.Message>
'. Một chuyển đổi rõ ràng tồn tại (bạn có bỏ lỡ một dàn diễn viên không?)
Ngoài ra, hàm tạo 'UserProject()', khởi tạo 'Tin nhắn', phải được đặt ở chế độ công khai, nếu không sẽ không thể truy cập được. –
Cảm ơn ... hoạt động ban đầu ... nhưng bây giờ tôi đang mắc kẹt ... Lỗi Không thể chuyển đổi hoàn toàn loại 'System.Collections.Generic.IEnumerable' thành 'System.Collections.Generic. Danh sách '. Một chuyển đổi rõ ràng tồn tại (bạn có bỏ lỡ một dàn diễn viên không?) –
Sử dụng ToList() ở cuối truy vấn của bạn –