asp.netasp.net-membershipasp.net-profiles

Linq members with profiles


I am trying to make a linq query to pull all users in the membership with their profiles. Here is what I have gotten already:

    Dim Mbrs = Membership.GetAllUsers

    Dim Pros = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All)

    Dim q = From mem In Mbrs Join pro In Pros On mem.UserName Equals
        pro.UserName Select mem.email, mem.username, mem.islockedout,
        pro.FirstName, pro.lastname, pro.city, pro.state, pro.zip, pro.type

If i take the profile stuff off the select, it will at least pull a list of members. What am I doind wrong??


Solution

  • Make a Class like this

    Imports Microsoft.VisualBasic
    
    Public Class Mems
    Public Property FirstName As String = ""
    Public Property LastName As String = ""
    Public Property Address1 As String = ""
    Public Property Address2 As String = ""
    Public Property City As String = ""
    Public Property State As System.Int32 = "0"
    Public Property Zip As String = ""
    Public Property Title As String = ""
    Public Property Phone As System.Decimal = "0"
    Public Property Id As System.Int32 = "0"
    Public Property UserType As System.Int32 = "0"
    Public Property SSN As System.Int32 = "0"
    Public Property Email As String = ""
    Public Property UserName As String = ""
    Public Property IsLockedOut As Boolean = False
    
    Private Loaded As Boolean = False
    Public Sub New(usr As String)
        GetMem(usr)
    End Sub
    Public Sub New()
    
    End Sub
    Public Sub Save()
        If Not Loaded Then Throw New Exception("No User Loaded")
        Dim mm = Membership.GetUser(UserName)
        mm.Email = Email
        Membership.UpdateUser(mm)
    
        Dim p As ProfileCommon = New ProfileCommon
        Dim pp = p.GetProfile(UserName)
        pp.Address1 = Address1
        pp.Address2 = Address2
        pp.City = City
        pp.FirstName = FirstName
        pp.Id = Id
        pp.LastName = LastName
        pp.Phone = Phone
        pp.SSN = SSN
        pp.State = State
        pp.Title = Title
        pp.UserType = UserType
        pp.Zip = Zip
        pp.Save()
    
    
    End Sub
    
    Public Sub GetMem(usr As String)
        'Try
        If usr.Length = 0 Then
            FirstName = "SYSTEM"
        Else
    
    
            Dim mm = Membership.GetUser(usr)
            Email = mm.Email
            IsLockedOut = mm.IsLockedOut
    
            Dim p As ProfileCommon = New ProfileCommon
            Dim pp = p.GetProfile(usr)
            Address1 = pp.Address1
            Address2 = pp.Address2
            City = pp.City
            FirstName = pp.FirstName
            Id = pp.Id
            LastName = pp.LastName
            Phone = pp.Phone
            SSN = pp.SSN
            State = pp.State
            Title = pp.Title
            UserType = pp.UserType
            Zip = pp.Zip
    
            UserName = usr
            Loaded = True
            'Catch ex As Exception
    
            ' End Try
        End If
    
    End Sub
    Public Function ChangeUsername(uTo As String) As Integer
        If Loaded Then
            Return ChangeUsername(UserName, uTo)
        Else
            Throw New Exception("No User Loaded")
        End If
    End Function
    Public Function ChangeUsername(uFrom As String, uTo As String) As Integer
    
        If Membership.FindUsersByName(uFrom).Count = 0 Then
            Throw New Exception("Original UserName Don't Exists")
        ElseIf Membership.FindUsersByName(uTo).Count = 1 Then
            Throw New Exception("Target UserName Don't Exists")
        Else
            Dim myDb As New MyDbDataContext
            Dim q = (From u In myDb.aspnet_Users Where u.UserName = uFrom And u.ApplicationId.Equals("72e4b882-e471-4f4b-b161-af0abbcb8487") Select u).Single
    
            q.UserName = uTo
            q.LoweredUserName = uTo.ToLower
            Try
                myDb.SubmitChanges()
                Return 1
            Catch ex As Exception
                Return 0
    
            End Try
        End If
    
    End Function
    
    End Class
    

    then you can call it from a functions like this

        Public Shared Function GetMems() As List(Of Mems)
        GetMems = New List(Of Mems)
        Dim mm = Membership.GetAllUsers
    
        For Each mbr As MembershipUser In mm
            GetMems.Add(New Mems(mbr.UserName))
        Next
    
        Return GetMems
    End Function