【发布时间】:2013-11-11 12:35:57
【问题描述】:
我有这两个 linq 查询:
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
};
我正在使用返回 JSON 的 Web API。我想合并这两个查询的输出。
我想重写此代码,使 cmets 和关注者数据应根据 ctime 和 startedfollowing 相对于时间进行合并(而不是合并)。如果用户有新评论,则评论应该排在第一位,如果关注者在前,关注者数据应该排在第一位。我不能使用 Union() 和 Concat() 因为首先两个类有不同的成员,其次我不想要两个要组合的 json 对象。
类似这样的:
{ //comments data },
{ //follower data},
{ //comments data },
{ //comments data },
{ //comments data },
{ //follower data}
那么这个任务怎么做呢?
【问题讨论】:
-
你知道
Union()吗? -
是的,我确实知道 Union() 和 Concat(),但不能使用它们,因为首先这两个类有不同的成员,其次我不希望两个 json 对象组合在一起。
-
听起来你必须full outer join你的两个查询,但我不确定。
-
我认为连接不起作用
-
除了将它们打印出来并使用胶带外,我不明白这两个结果集如何相互关联......它们是否受众多
userids 之一的约束?
标签: c# asp.net sql linq linq-to-sql