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

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.