简体   繁体   English

如何结合这两个linq查询的输出?

[英]How to combine the output of these two linq queries?

I want to combine the output of these two queries,combo1 and combo2. 我想结合这两个查询的输出,combo1和combo2。 As per now i am able to write a code something like this: 到目前为止,我能够编写如下代码:

        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;

But on the second last line where i am performing a union, i am getting these errors : 但是在我执行工会的第二行,我收到了这些错误:

Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Linq.IQueryable'. 错误1无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'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' 错误2实例参数:无法从>'System.Linq.IQueryable'转换为>'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 错误3'System.Linq.IQueryable'不包含'Union'的定义和最佳扩展方法重载'System.Linq.ParallelEnumerable.Union(System.Linq.ParallelQuery,System.Collections.Generic.IEnumerable)'有一些无效的参数

Where i am wrong, what should i do to successfully combine these two queries? 哪里我错了,我该怎么做才能成功地结合这两个查询?

You could return an anonymous type: 您可以返回匿名类型:

return Json(new {
    Comments = combo1,
    Followers = combo2
});

If you want to pass them around as parameters in methods, you should create a class which contains the two collections as properties: 如果要将它们作为方法中的参数传递,则应创建一个包含两个集合作为属性的类:

public class CommentsAndFollowersDTO
{
    public IEnumerable<TCommentDTO> Comments { get; set; }

    public IEnumerable<TfollowerDTO> Followers { get; set; }
}

I don't think you can Union a collection of two different types. 我不认为你可以联合两种不同类型的集合。 You can always take the two queries and join them into an anonymous object like so 您始终可以将这两个查询加入到一个匿名对象中,就像这样

var q = from c1 in combo1
        from c2 in combo2
        select new { 
            Comments = c1,
            Followers = c2
        }

I'm not sure what your desired output is, but if you're not looking to make a new class with a property to hold both lists, this way would work. 我不确定你想要的输出是什么,但是如果你不打算创建一个带有属性的新类来保存两个列表,这种方式就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM