2009-01-07 2 views
8

Ra khỏi hộp, System.Web.Security.Membership thực hiện một vài phương pháp tìm kiếm:Làm thế nào để thực hiện tốt nhất tìm kiếm tùy chỉnh trên một nhà cung cấp Membership

  • FindUsersByEmail
  • FindUsersByName

tôi đang sử dụng WSAT project from CodePlex để quản lý cơ sở dữ liệu Thành viên của tôi. Công cụ này thực hiện các thuộc tính profile bổ sung trong một lớp ProfileCommon.

Giả sử tôi có tài sản được gọi là Công ty trong hồ sơ của người dùng.

Tôi cần triển khai phương pháp tìm kiếm tùy chỉnh để tìm kiếm trên thuộc tính Công ty và tôi muốn thực hiện tất cả trong mã. Không muốn viết một thủ tục lưu sẵn (vì tất cả các thuộc tính hồ sơ được lưu trữ trong cột cơ sở dữ liệu 1 trong công cụ WSAT).

Something như thế này rõ ràng không phải là cách đúng đắn để làm điều đó, nhưng ở đây nó là chỉ cần chứng minh truy cập vào thuộc tính profile người dùng:

private MembershipUserCollection SearchByFirm(string firmName, MembershipUserCollection allRegisteredUsers) 
{ 
    MembershipUserCollection searchResults = new MembershipUserCollection(); 

    foreach (MembershipUser user in allRegisteredUsers) 
    { 
     ProfileCommon profile = Profile.GetProfile(user.UserName); 
     if (profile.Firm.ToLowerInvariant().Contains(firmName.ToLowerInvariant())) 
     { 
      searchResults.Add(user); 
     } 
    } 
    return searchResults; 
} 

Tôi có thể tắt chức năng này vào một vài tiện ích LINQ?

+0

Nhận thấy MembershipUserCollection không thực hiện IEnumerable hoặc, làm phức tạp mọi thứ một chút –

Trả lời

3

Có một số trợ giúp từ một đồng nghiệp giỏi về LINQ. Thách thức ở đây là MembershipUserCollection không thực hiện IEnumerable < T> (!).

 List<MembershipUser> searchResults = allUsers.Where(user => 
     Profile.GetProfile(user.UserName).Firm.ToLowerInvariant() 
     .Contains(firmName.ToLowerInvariant())).ToList(); 

trong trường hợp này, tất cảNgười dùng là Danh sách mà tôi phải điền vào các mục trong bộ sưu tập Thành viên.GetAllUsers().

22

Bạn không thể chỉ truyền nó?

IEnumerable<MembershipUser> searchResults = Membership.GetAllUsers().Cast<MembershipUser>(); 

Hy vọng điều này sẽ giúp bạn kẻ

+0

Tôi đã sử dụng cách tiếp cận này và nó hoạt động hoàn hảo. Cảm ơn! –

+0

+1 cho ngắn gọn và hiệu quả. –

0

Chỉ cần cho các hồ sơ tôi đã tạo phương pháp này mở rộng mà tôi nghĩ nó kinda hoạt động:

namespace WebDibaelsaMVC.Utils.MembershipUserCollectionExtensions 
{ 
    public static class MembershipUserCollectionExtensions 
    { 
     public static IEnumerable<MembershipUser> Where(this MembershipUserCollection userCollection,Func<MembershipUser,bool> func) 
     { 
      foreach (MembershipUser membershipUser in userCollection) 
      { 
       if (func(membershipUser)) 
        yield return membershipUser; 
      } 
     } 
    } 
} 

Nó cũng chuyển đổi các MembershipUserCollection một IEnumerable<MembershipUser> vì vậy tất cả khác LINQ phương pháp làm việc sau đó.

0

Không có chức năng tích hợp được cung cấp bởi microsoft. Dưới đây là ví dụ về thành viên thành viên tìm kiếm với UserName và Email Địa chỉ
Ví dụ:
Chỉ cần sao chép dưới chức năng và thực hiện nó - Xong ...

Public List<MembershipUser> SearchMembershipUser(string strUserName, String strEmail) 
     {   
      IEnumerable<MembershipUser> MUser; 
      if ((!string.IsNullOrEmpty(strUserName) || !string.IsNullOrEmpty(strEmail))) 
      { 
       if (!string.IsNullOrEmpty(strUserName) && !string.IsNullOrEmpty(strEmail)) 
       { 
        MUser = Membership.GetAllUsers().Cast<MembershipUser>() 
         .Where(x => x.UserName != CurrentUser && x.UserName == strUserName && x.Email == strEmail); 
       } 
       else if (!string.IsNullOrEmpty(strUserName)) 
       { 
        MUser = Membership.GetAllUsers().Cast<MembershipUser>() 
         .Where(x => x.UserName != CurrentUser && x.UserName == strUserName); 
       } 
       else 
       { 
        MUser = Membership.GetAllUsers().Cast<MembershipUser>() 
         .Where(x => x.UserName != CurrentUser && x.Email == strEmail); 
       } 
      } 
      else 
      { 
       MUser = Membership.GetAllUsers().Cast<MembershipUser>().Where(x => x.UserName != CurrentUser); 
      } 
      return MUser.OrderBy(x => x.UserName).ToList(); 
     }