使用 Linq 查询成员资格

发布于 2024-08-30 17:37:11 字数 246 浏览 2 评论 0原文

我不是一个经验丰富的程序员,我需要查询asp.net mvc中提供的Membership User Collection。

我希望会员能够添加其他会员为好友,我创建了一个添加好友表。

ID, 会员ID, Friend_MemberId, DateAdded

我想显示未添加到此列表中的成员列表(例如过滤已经存在的朋友),但无法使用 linq 查询,任何人都可以建议一种方法、链接、文章,扩展 memebership 类会更好吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2024-09-06 17:37:11

有很多方法可以解决这个问题。

我们来看看其中一个。

您可以在此处下载可用的 VS2008 解决方案。该示例不是 MVC 项目,但无论如何,成员资格提供程序的工作方式都是相同的。

规定:

  • 您正在使用默认的默认 SqlProviders
  • 您知道如何使用 ASPNETDB 添加 ADO.Net 实体模型
  • 您的 ASPNETDB 为一个应用程序提供服务,默认为“/”。如果情况并非如此,那么您已经具备了调整以下指南所需的知识。

在 ASPNETDB 中创建 Friends 表:

下面假设您使用的是在 app_data 中创建的默认 ASPNETDB。如果没有,那么您已经创建并连接到另一个数据库,只需获取您需要的即可。

  • 在解决方案资源管理器中选择您的项目,单击解决方案资源管理器顶部的“显示所有文件”图标,展开“App_Data”文件夹并右键单击>打开 ASPNETDB.MDF。

  • 在服务器资源管理器中,您将看到 ASPNETDB。

  • 项目>添加新项目>文本文件>Friends.sql

  • 在下面粘贴查询。节省。

  • 在编辑器中右键单击>连接>连接>选择 ASPNETDB

  • 在编辑器中单击右键>执行 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

将 ADO.Net 实体数据模型添加到您的项目中,并至少包括以下内容:

  • Friends

视图

  • vw_aspnet_MembershipUsers

查询示例:

注意:我是绝不是 Linq 大师。这些查询工作正常,生成的 sql 对我来说似乎不合理,但我确信有人会对查询的可能优化提供有用的建议。

// 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文