ASP.NET: Using LINQ to Entities to retrieve and update a single value from a single record

Hello –

I am building my first web application, using ASP.NET 4.0 with VB. I'm also using Entity Framework 4.0 and LINQ to Entities, both of which are new to me as well.

I have created some pages that use an Entity Data Source to populate a GridView or DetailView , but this question involves a new page that just needs to work with a single value from a single record. Instead of creating an Entity Data Source, I tried different things in the code and eventually found a way to use LINQ 2 EF to both retrieve and update the required value.

Question:  I have a solution that is working, but since I am learning I'm asking for you to review it to see if this is okay or if there's a better way. Better could be in terms of speed/performance once it is deployed, overall efficiency, coding practices, or anything else I haven't thought of.

Here is the code behind for this page (I'll explain the objective below):

Partial Class iFAapp_AS4
    Inherits System.Web.UI.Page
    Dim ctxAcctSettings As New EFmodels.edmAcctSettings

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack = False Then
            Dim sortSet = (From s In ctxAcctSettings.iFAacctPrefs
                          Where s.AcctID = Profile.iFAacctID And s.PrefName = "NameSort"
                          Select s).Single
            Dim theVal As String = sortSet.PrefValue

            If theVal = 1 Then
                Me.rdoSort1.Checked = True
                Me.rdoSort2.Checked = True
            End If
        End If

    End Sub

    Protected Sub rdoSort1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoSort1.CheckedChanged
        If rdoSort1.Checked = True Then
            SetSort(Profile.iFAacctID, 1)
        End If
    End Sub

    Protected Sub rdoSort2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoSort2.CheckedChanged
        If rdoSort2.Checked = True Then
            SetSort(Profile.iFAacctID, 2)
        End If
    End Sub

    Protected Sub SetSort(ByVal intAcctID As Integer, ByVal strVal As String)
        'update the AcctPref record for name sort
        Dim sortSet = (From s In ctxAcctSettings.iFAacctPrefs
                       Where s.AcctID = intAcctID And s.PrefName = "NameSort"
                       Select s).Single
        'set the new value
        sortSet.PrefValue = strVal

        panelMsg.Visible = True
        lblMsg.Text = "Your selection has been saved. {NameSort saved as: " & strVal & "}"
    End Sub

End Class

Open in new window

This web app uses ASP's membership feature, and it will be offered as software-as-a-service, so each user will work with only his data. This page is an "Account Settings" page, where the user can change personalize certain preferences for the application.

The only setting being addressed so far is the name sort preference, where the user can select whether his contact records are displayed as "firstname lastname" or "lastname, firstname".  The page contains a group of two radio buttons to present these two choices.

The preference is stored in a table and can be identified by the user's AcctID combined with the PrefName "NameSort".  The Page_Load event retrieves the current preference and sets the radio buttons accordingly. If the user checks a different radio button, the code calls "SetSort" which saves the change to the database.

As mentioned, the code above is working. But this is a new technique with LINQ for me so I want to have it reviewed by experts! Many of the future pages of this app will need to lookup a single value in the preferences table, and this particular page will have two additional sections to manage two other preferences, so I want to learn if this approach is okay as is or can be enhanced.

Thanks in advance for any input you can provide!
Who is Participating?
Bob LearnedConnect With a Mentor Commented:
I don't see anything that, on the surface, would need to be changed.

Here is some food for thought:

You can use a single Lambda expression, which is composable, for the LINQ query.  I like to use typed arguments for readability, but that is a personal choice.


Dim sortSet = (From s In ctxAcctSettings.iFAacctPrefs
                          Where s.AcctID = Profile.iFAacctID And s.PrefName = "NameSort"
                          Select s).Single

Open in new window

would be something like this:

Dim sortSet As List(Of iFAacctPref) = 
          .Where(Function(x) s.AcctID = Profile.iFAacctID AndAlso 
                                         s.PrefName = "NameSort")

Open in new window

JMS1965Author Commented:
Thanks for the input ... good to know I'm on the right tract and that this approach is okay moving forward. I'll start to work with the Lambda expressions as well.

Thanks again!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.