Sunday 28 September 2014

Lightswitch DLookup Function



I remember when I was still using MS Access & VB to create business type applications, there was a function in VB called DLookup. I had made extensive use of this function as it made it very easy to look up a value from related or unrelated tables.

For instance, lets say you created a log in page in your access application, you would use DLookup to verify if the username and password existed, and then check whether the password was correct, and do the authentication.

So when I started I was looking for a similar function in LS vb.net. I was amazed at how easy this task was and the flexibility it gave you as the programmer.

For instance, lets say you have a textbox, and you wanted to check whether the value that the user typed in the textbox existed in a table, you would use the following:

    If Me.Textbox = Me.tbl_Users.Where(Function(c) c.username = Me.Textbox).Count > 0 then
        ShowMessageBox("User exists in table")
    Else
        ShowMessageBox("User does not exists in table")
    End If

Thats it.

There is a couple of variations on this function that I will show you once I have updated this post.

Variation 1:

So lets say you wanted to assign a value from a table to a variable the function would look like below:

Dim myVariable = (From i In Me.DataWorkspace.yourTable Where i.itemId = 123 Select I).First

In the above code the variable myVariable will be assigned the value of the field in yourTable where the itemId is equal to 123.

You could ofcourse assign any variable in any table by just changing the relevant table and value you need in the code.

No comments:

Post a Comment