我想结合这两个查询的输出,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; }
}

09-25 18:08
查看更多