简体   繁体   中英

How to combine the output of these two linq queries?

I want to combine the output of these two queries,combo1 and 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'. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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