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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:33:10+00:00 2026-05-14T16:33:10+00:00

I am not a experienced programmer, I need to query the Membership User Collection

  • 0

I am not a experienced programmer, I need to query the Membership User Collection provided in asp.net mvc.

I want the members be able to add other members as friends, I have created a added friend table.

Id,
MemberId,
Friend_MemberId,
DateAdded

I want to display a list of Members which are not added to this list (like filter already existing friends), but unable to query using linq, can anyone suggest a way, links, articles, would it be better to extend memebership class.

  • 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-14T16:33:11+00:00Added an answer on May 14, 2026 at 4:33 pm

    There are quite a number of ways you could go about this.

    Let’s examine one.

    You can download the working VS2008 solution here. The example is not an MVC project, but the membership provider works the same regardless.

    Stipulations:

    • You are using default default SqlProviders
    • You know how to add an ADO.Net Entity Model using the ASPNETDB
    • Your ASPNETDB serves one application, the default ‘/’. If this is not the case then you will already have the necessary knowledge to adjust the following guidance.

    Create the Friends table in the ASPNETDB:

    The following assumes that you are using the default ASPNETDB that is created in app_data. If not, then you have already created and connected to another DB, just take what you need.

    • Select your project in Solution Explorer, click ‘show all files’ icon at the top of Solution Explorer, expand ‘App_Data’ folder and right-click>Open ASPNETDB.MDF.

    • In Server Explorer you will see your ASPNETDB.

    • Project>Add New Item>Text File>Friends.sql

    • Paste query below. Save.

    • Right click in editor>Connection>Connect>select ASPNETDB

    • Right click in editor>Execute SQL

    Friends.sql

    /* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
    BEGIN TRANSACTION
    SET QUOTED_IDENTIFIER ON
    SET ARITHABORT ON
    SET NUMERIC_ROUNDABORT OFF
    SET CONCAT_NULL_YIELDS_NULL ON
    SET ANSI_NULLS ON
    SET ANSI_PADDING ON
    SET ANSI_WARNINGS ON
    COMMIT
    BEGIN TRANSACTION
    GO
    CREATE TABLE dbo.Friends
        (
        Id int NOT NULL IDENTITY (1, 1),
        MemberId uniqueidentifier NOT NULL,
        Friend_MemberId uniqueidentifier NOT NULL,
        DateAdded datetime NOT NULL
        )  ON [PRIMARY]
    GO
    ALTER TABLE dbo.Table1 ADD CONSTRAINT
        DF_Table1_DateAdded DEFAULT GetDate() FOR DateAdded
    GO
    ALTER TABLE dbo.Table1 ADD CONSTRAINT
        PK_Table1 PRIMARY KEY CLUSTERED 
        (
        Id
        ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    
    GO
    ALTER TABLE dbo.Table1 ADD CONSTRAINT
        IX_Table1 UNIQUE NONCLUSTERED 
        (
        MemberId,
        Friend_MemberId
        ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    
    GO
    ALTER TABLE dbo.Table1 SET (LOCK_ESCALATION = TABLE)
    GO
    COMMIT
    

    Add an ADO.Net Entity Data Model to your project and include, at the minimum, the following:

    Tables

    • Friends

    Views

    • vw_aspnet_MembershipUsers

    Query Example:

    NOTE: I am by no means a Linq guru. These queries work fine and the generated sql does not seem unreasonable to me, but I am sure there is someone who will have helpful suggestions regarding possible optimizations of the queries.

    // there are 3 users: User1, User2 and User3
    
    // User1 has one friend, User2
    
    string username = "User1"; // this would be the User.Identity.Name (currently logged in user)
    
    // 
    
    vw_aspnet_MembershipUsers[] friends;
    vw_aspnet_MembershipUsers[] notFriends;
    
    using (var ctx = new ASPNETDBEntities())
    {
        // get the userId
        Guid userId = ctx.vw_aspnet_MembershipUsers.First(m => m.UserName == username).UserId;
    
    
    
    
        var usersFriendsQuery = from friend in ctx.Friends
                                join muser in ctx.vw_aspnet_MembershipUsers on friend.Friend_MemberId equals muser.UserId
                                where friend.MemberId == userId
                                select muser;
    
    
        friends = usersFriendsQuery.ToArray();
    
        Debug.Assert(friends.Count()==1);
        Debug.Assert(friends[0].UserName=="User2");
    
    
    
        var usersNotFriendsQuery = from muser in ctx.vw_aspnet_MembershipUsers
                                   where ctx.vw_aspnet_MembershipUsers.Any(m =>
                                       ctx.Friends.FirstOrDefault(friend =>
                                           // include self in excluded members
                                           (muser.UserId == userId)
                                           ||
                                               // include already friends in excluded members
                                           (friend.MemberId == userId && friend.Friend_MemberId == muser.UserId)
                                           ) == null)
                                   select muser;
    
    
        notFriends = usersNotFriendsQuery.ToArray();
    
        Debug.Assert(notFriends.Count() == 1);
        Debug.Assert(notFriends[0].UserName == "User3");
    }
    
    // do something interesting with friends and notFriends here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not the most experienced GUI programmer, so bear with me here. I
First of all: I am not an experienced ClearCase user, but I have lots
( Updated a little ) I'm not very experienced with internationalization using PHP, it
Sorry, I'm not terribly experienced with Ant. I like the eclipse Export ant buildfile
I'm running my C++ program in gdb. I'm not real experienced with gdb, but
Our team is experienced in web development (tomcat/Oracle) but not with AJAX on our
I do not have any experience with programming fractals. Of course I've seen the
I have to update old projects at work. I do not have any experience
In my experience, they are not a good idea because they can result in
Not very technical, but... I have to implement a bad words filter in a

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.