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 691235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:31:12+00:00 2026-05-14T02:31:12+00:00

I have a query that pulls back a user’s feed which is essentially all

  • 0

I have a query that pulls back a user’s “feed” which is essentially all of their activity. If the user is logged in the query will be filtered so that the feed not only includes all of the specified user’s data, but also any of their friends.

The database structure includes an Actions table that holds the user that created the action and a UserFriends table which holds any pairing of friends using a FrienderId and FriendeeId column which map to UserIds.

I have set up my LINQ query and it works fine to pull back the data I want, however, I noticed that the query gets turned into X number of CASE clauses in profiler where X is the number of total Actions in the database. This will obviously be horrible when the database has a user base larger than just me and 3 test users.

Here’s the SQL query I’m trying to achieve:

select * from [Action] a 
where a.UserId = 'GUID'
OR a.UserId in 
    (SELECT FriendeeId from UserFriends uf where uf.FrienderId = 'GUID')
OR a.UserId in 
    (SELECT FrienderId from UserFriends uf where uf.FriendeeId = 'GUID')

This is what I currently have as my LINQ query.

feed = feed.Where(o => o.User.UserKey == user.UserKey 
      || db.Users.Any(u => u.UserFriends.Any(ufr => ufr.Friender.UserKey == 
                                             user.UserKey && ufr.isApproved)
      || db.Users.Any(u2 => u2.UserFriends.Any(ufr => ufr.Friendee.UserKey == 
                                                user.UserKey && ufr.isApproved)
      )));

This query creates this:
http://pastebin.com/UQhT90wh

That shows up X times in the profile trace, once for each Action in the table. What am I doing wrong? Is there any way to clean this up?

  • 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-14T02:31:12+00:00Added an answer on May 14, 2026 at 2:31 am

    I would split the query into two queries,

    1. Find all friends for the user, select their user keys and put them into a list.
    2. Filter the feed according to the userkey and the keys of her.

    Here is my shot at it without knowing your exact itnerfaces and ojbects, but it shows the concept:

        var friends = db.UserFriends
                        .Where(x => x.isApproved && (
                                        x.Friender.UserKey == user.userKey || 
                                        x.Friendee == user.userKey
                                    )
                        )
                        .Select(x => x.userKey)
                        .Distinct()
                        .ToList();
        feed = feed.Where(x => x.userKey == user.userKey || friends.Contains(x.UserKey));

    This should yield a query similar to this

    SELECT ... 
    FROM feed 
    WHERE userKey == 'userkey1' OR userKey in ('userKey2', 'userKey3', ...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 347k
  • Answers 347k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I understand your confusion and it took me some time… May 14, 2026 at 6:19 am
  • Editorial Team
    Editorial Team added an answer Yes it is, OLAP will keep a copy until the… May 14, 2026 at 6:19 am
  • Editorial Team
    Editorial Team added an answer Its a good idea, similar to the icons used on… May 14, 2026 at 6:19 am

Related Questions

I have all the time zone information that is needed and it saves all
Ok, please bear with me as I can be a bit of a wood
I am desperately trying to use LinqKits PredicateBuilder to allow the user to enter
Hey all, I am wondering what the best way to do the following is:
Hopefully I can do the problem justice, because it was too difficult to summarise

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.