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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:21:06+00:00 2026-05-23T00:21:06+00:00

There is a table called UserFriends that holds records for users’ friendships. For each

  • 0

There is a table called UserFriends that holds records for users’ friendships.
For each friendship, there is just one record,

User1ID  User2ID IsConfirmed
1        2       true

which is equal in terms of business logic to

User1ID  User2ID IsConfirmed
2        1       true

but both can’t happen for one pair.

What is the most efficient (yet readable and not involving plain SQL) Entity Framework query to determine if user A is a friend of user B, considering we don’t know which of them is in first or second column?

My attempt is plain and obvious:

public bool AreFriends (int user1Id, int user2Id)
{
    return MyObjectContext.UserFriends
        .Any (uf => uf.IsConfirmed && (
                    (uf.UserID == user1Id && uf.FriendUserID == user2Id)
                    || (uf.UserID == user2Id && uf.FriendUserID == user1Id)
        ));
}

Is there a better way than || here?

  • 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-23T00:21:06+00:00Added an answer on May 23, 2026 at 12:21 am

    I think if the User1ID and User2ID columns both are primary key columns this query cause an index seek and is so efficient. Tuning of a query when it is critical, without analysing the execution plan, is inefficient. For important queries, I suggest you use SQL Server (or any DBMS) to write and analyse your queries and then convert these queries to LINQ queries. It is not hard.

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

Sidebar

Related Questions

I have one table called gallery . For each row in gallery there are
So i have database with a table called users.. and in that table there
I have a table called items. IN this table there are many columns. One
Assuming that there's a table called tree_node , that has the primary key called
I have a MySQL table called employees . There's another table called shifts that
There is a certain job that will insert and update this table called ContactInfo
i need to do this : There is a table called table1 it has
i need to do this : There is a table called table1 it has
I'm looking through my organization's database, and there is a table called dbo.Account_Alerts, and
I have a table TIME_TABLE on Oracle 10.6. There is a field called Start_Date

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.