[英]How to combine the output of these two linq queries?
我想结合这两个查询的输出,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;
但是在我执行工会的第二行,我收到了这些错误:
错误1无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Linq.IQueryable'。 存在显式转换(您是否错过了演员?)
错误2实例参数:无法从>'System.Linq.IQueryable'转换为>'System.Linq.ParallelQuery'
错误3'System.Linq.IQueryable'不包含'Union'的定义和最佳扩展方法重载'System.Linq.ParallelEnumerable.Union(System.Linq.ParallelQuery,System.Collections.Generic.IEnumerable)'有一些无效的参数
哪里我错了,我该怎么做才能成功地结合这两个查询?
您可以返回匿名类型:
return Json(new {
Comments = combo1,
Followers = combo2
});
如果要将它们作为方法中的参数传递,则应创建一个包含两个集合作为属性的类:
public class CommentsAndFollowersDTO
{
public IEnumerable<TCommentDTO> Comments { get; set; }
public IEnumerable<TfollowerDTO> Followers { get; set; }
}
我不认为你可以联合两种不同类型的集合。 您始终可以将这两个查询加入到一个匿名对象中,就像这样
var q = from c1 in combo1
from c2 in combo2
select new {
Comments = c1,
Followers = c2
}
我不确定你想要的输出是什么,但是如果你不打算创建一个带有属性的新类来保存两个列表,这种方式就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.