我不是一个有经验的程序员,我需要查询在asp.net mvc的提供的会员用户采集。会员查询使用LINQ
我希望成员能够将其他成员添加为朋友,我创建了一个添加的朋友表。
标识, 成员Id, Friend_MemberId, DateAdded
我想显示未添加到这个列表(如过滤器已经存在的朋友)成员的名单,但无法使用LINQ查询,任何人都可以建议一种方式,链接,文章,延长美世纪班会更好吗?
我不是一个有经验的程序员,我需要查询在asp.net mvc的提供的会员用户采集。会员查询使用LINQ
我希望成员能够将其他成员添加为朋友,我创建了一个添加的朋友表。
标识, 成员Id, Friend_MemberId, DateAdded
我想显示未添加到这个列表(如过滤器已经存在的朋友)成员的名单,但无法使用LINQ查询,任何人都可以建议一种方式,链接,文章,延长美世纪班会更好吗?
有相当数量的方式,你可以去这个问题。
让我们来看看一个。
您可以下载工作VS2008解决方案here。该示例不是MVC项目,但成员资格提供程序的工作原理是相同的。
规定:
在ASPNETDB创建好友表:
以下假定您使用的是在app_data文件创建的默认ASPNETDB。如果没有,那么你已经创建并连接到另一个数据库,只需要你需要的。
选择在Solution Explorer中的项目,点击“显示所有文件”,在Solution Explorer中的顶部图标,依次展开“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。网络实体数据模型到您的项目,包括,至少,以下内容:
表
查看
查询示例:
注意:我决不是Linq guru。这些查询工作正常,生成的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
在这一个方面有何进展? – 2010-05-02 13:40:59