0

I am able to write this code for a Web Api. In this code i have written tow queries and combined both in a way that comments and follower data should be merged (not combined) with respect to time based on ctime and startedfollowing. If a user has a new comment, the comment should come first and if the follower is first, the follower data should come first.

 public IQueryable<Object> GetCommentsandFollowActivityCommnets()
    {

        var combo1 = from c in db.comments
                    join p in db.picturedetails 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 result1 = from c in combo1
          select new UserTimeLineDTO
          { SortKey = c.ctime, Member =c};

          var result2 = from c in combo2
          select new UserTimeLineDTO{ SortKey = c.startedfollowing, Member = c };

            var result = result1.Concat(result2).OrderBy(x =>x.SortKey).Select(x => x.Member);

            return result; 

    }

The code is not giving any compile time error. It runs fine in compiler, but on run time i am getting an exception:

DbUnionAllExpression requires arguments with compatible collection ResultTypes.

How to remove this exception?

0

1 Answer 1

1

As a workaround, I would try to evaluate the last expression in memory:

      var result1 = (from c in combo1
      select new UserTimeLineDTO
      { SortKey = c.ctime, Member =c}).ToList();

      var result2 = (from c in combo2
      select new UserTimeLineDTO{ SortKey = c.startedfollowing, Member = c }).ToList();

      var result = result1.Concat(result2).OrderBy(x =>x.SortKey).Select(x => x.Member);

The union should now succeed, as well as ordering and the final projection.

Sign up to request clarification or add additional context in comments.

1 Comment

.ToList forces the evaluation and materializes objects in memory. This way the Concat is evaluated in memory rather than at the database side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.