我想合并这两个查询的输出,combo1 和 combo2。按照现在我能够编写这样的代码:
var combo1 = from c in db.comments
join p in db.picture on c.targetpictureid equals p.idpictures
join u in db.users on c.iduser equals u.iduser
select new TCommentDTO
{
idcomments=c.idcomments,
comment1 = c.comment1,
targetpictureid = c.targetpictureid,
ctime = c.ctime,
iduofpic=p.iduser,
iduofcommentor=c.iduser,
profilepicofcommentor=u.profilepic,
usernameofcommentor=u.username,
picFilename=p.picFilename,
picTitle=p.picTitle
};
var combo2 = from f in db.followers
join u in db.users on f.iduser equals u.iduser
select new TfollowerDTO
{
idfollowers = f.idfollowers,
iduser = f.iduser,
targetiduser = f.targetiduser,
startedfollowing = f.startedfollowing,
unoffollower = u.username,
ppoffollower = u.profilepic,
status = u.status
};
var resultantcombo =combo1.Union(combo2);
return resultantcombo;
但是在我执行合并的倒数第二行,我遇到了这些错误:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)
Error 2 Instance argument: cannot convert from >'System.Linq.IQueryable' to >'System.Linq.ParallelQuery'
Error 3 'System.Linq.IQueryable' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.ParallelEnumerable.Union(System.Linq.ParallelQuery, System.Collections.Generic.IEnumerable)' has some invalid arguments
我哪里错了,我应该怎么做才能成功组合这两个查询?
请您参考如下方法:
你可以返回一个匿名类型:
return Json(new {
Comments = combo1,
Followers = combo2
});
如果你想在方法中将它们作为参数传递,你应该创建一个包含两个集合作为属性的类:
public class CommentsAndFollowersDTO
{
public IEnumerable<TCommentDTO> Comments { get; set; }
public IEnumerable<TfollowerDTO> Followers { get; set; }
}
