所以,我在我的应用程序中设置了ASP成员资格。我还有一个单独的用户表来管理非会员相关数据。在用户管理期间,我需要确保我的应用程序查询两个表。我有以下控制器返回已批准用户的列表,但似乎必须有一个更简单的方法来实现这一点。有什么更好的方法呢?
QuoteExchangeDB _db = new QuoteExchangeDB();
[MyAuthorize(Roles = "Administrator")]
public ActionResult Admin()
{
MembershipUserCollection agents = Membership.GetAllUsers();
IEnumerable<MembershipUser> unfiltered = agents.Cast<MembershipUser>();
var filtered = unfiltered.Where(u => u.IsApproved);
List<User> users = new List<User>();
foreach (var item in filtered)
{
if (item.IsApproved)
{
Guid guid = (Guid)item.ProviderUserKey;
users.Add(_db.Users.Single(u => u.MembershipGuid.Equals(guid)));
}
}
return View(users);
}
答案 0 :(得分:2)
看起来你可能想在LINQ中进行简单的连接。
var filtered = Membership.GetAllUsers().Cast<MembershipUser>().Where(u => u.IsApproved);
var users = from f in filtered
join u in _db.Users on ((Guid)f.ProviderUserKey) equals u.MembershipGuid
select u;
你甚至可以做出一个声明:
var users = from f in Membership.GetAllUsers().Cast<MembershipUser>()
join u in _db.Users on ((Guid)f.ProviderUserKey) equals u.MembershipGuid
where f.IsApproved
select u;
编辑:鉴于我不确定如何将IEnumerable与IQueryable一起加入可能会影响事件/导致此实例中出现问题,here's a blog about doing that。