Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:27:00+00:00 2026-05-27T10:27:00+00:00

I have a SQl query working fine, I need to convert it to LINQ

  • 0

I have a SQl query working fine, I need to convert it to LINQ and got some problems.

My working SQL query:

select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
where d.UserID = 2
/* friends */
Union
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
join Friends as fr 
on d.UserID = fr.FriendID 
where fr.UserID = 2
/* following */
Union
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
join Followers as fl 
on d.UserID = fl.UserID
where fl.FollowerID = 2
/* ordenando por UpdateTime desc */
order by 3 desc

What I tried based on my own previous question:

var friends = (from u in db.User
                       join f in db.Friends
                       on u.ID equals f.FriendID
                       where f.UserID == userset.ID
                       orderby u.Nickname
                       select new FriendsSet { ID = u.ID, Nickname = u.Nickname, Thumbnail = u.Thumbnail }).ToList();

var followers = (from u in db.User
                         join f in db.Followers
                         on u.ID equals f.FollowerID
                         where f.UserID == userset.ID
                         orderby u.Nickname
                         select new FriendsSet { ID = u.ID, Nickname = u.Nickname }).ToList();

var diaryPosts = (from d in db.DiaryPosts
                              join e in db.EstadosDeAlma
                              on d.EstadosDeAlmaID equals e.ID
                              join u in db.User
                              on d.UserID equals u.ID
                              where d.UserID == userset.ID
                              select new DiaryPostsSet { 
                                  PostID = d.ID,
                                  EstadoDeAlmaID = e.ID,
                                  EstadoDeAlma = e.Title,
                                  Author = u.Nickname,
                                  Thumbnail = u.Thumbnail,
                                  UserID = u.ID,
                                  IsDuplicated = d.IsDuplicated,
                                  FriendID = d.FriendID,
                                  FriendName = u.Nickname,
                                  Time = d.UpdateTime,
                                  MessagesCount = d.FriendMessages.Count(m => m.DiaryPostsID == d.ID)
                              }).Take(6).ToList();

var diaryPostsUnion = diaryPosts.Union(friends).Union(followers).OrderBy(d => d.UpdateTime);

The errors I get:

'System.Collections.Generic.List<MvcWebRole1.Models.DiaryPostsSet>' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.ParallelEnumerable.Union<TSource>(System.Linq.ParallelQuery<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments

Instance argument: cannot convert from 'System.Collections.Generic.List<MvcWebRole1.Models.DiaryPostsSet>' to 'System.Linq.ParallelQuery<MvcWebRole1.Models.FriendsSet>'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:27:01+00:00Added an answer on May 27, 2026 at 10:27 am

    You’re trying to union a sequence of one type with a sequence of another type. That doesn’t make sense – unions have to be over the same types, basically.

    It’s not really clear what you’re trying to do here, but I suspect you don’t want a union.

    Oh, and if you want all of this to happen in the database, get rid of the ToList calls.

    EDIT: Okay, it’s hard to see exactly how your LINQ queries correspond to your SQL queries, but basically your “select” clauses should be something like:

    from d in db.DiaryPosts
    // stuff here
    select new { UserID = d.ID, d.UpdateTime, d.Content }
    

    Use the exact same anonymous type in each query – the same property names and types, in the same order – and you should be able to use the Union method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq to SQL query that was working just fine with SQL
I have a Linq query which is working fine but i have noticed when
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have a SQL query (MS Access) and I need to add two columns,
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have a complex query and it was working fine before adding one more
I have this mysql query using PHP and first part is working fine, but
I have a query that is working fine when I run in through phpMyAdmin
I have the following dynamic query which is working fine without the WHERE clause,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.