none
VB.Net: Using LINQ to return a cell value? RRS feed

  • Question

  • Hello,

    I am trying to return a user's email address which is located in a data set using LINQ. Here is the code I tried using. Basically a user will log on and this should return that user's email address.

    Dim realPass = From pass In BeanBagDataSet.Users _
    Where pass.Username = LogIn.currentUser _
    Select pass.Email

    The problem is that I want the code to return a single cell value as a string (The user's email address). I do not know how to do this. Can anyone help? It seems really simple but I just don't know the syntax for it. Thanks.
    Sunday, April 24, 2011 5:40 AM

Answers

  • Hi Kozzer;

    In the below query BeanBagDataSet.Users represents the collection of rows in the Users table and for every iteration of that collection pass represents a single row of the collection. In the Where clause you compare the column Username in the row with a variable LogIn.currentUser. In the Select clause is where you select what you want to return. If you return pass as in, Select pass, you are retuning the complete row. If you just want a single column returned then you select that one column, for example, Select pass.Email, which states to select from the current row the column Email. By add the FirstOrDefault() method, ( query body ).FirstOrDefault(), you are selecting the first item in the collection of the return set if one exist. If there are no values in the collection a default value is returned. That value depends on the data type of the returned value, for example in this case you are returning a string value of Email so if no values are in the return set a String.Empty is returned. If the returned value were of a reference type then a null value would be returned.

    Dim realPass = (From pass In BeanBagDataSet.Users _
        Where pass.Username = LogIn.currentUser _
        Select pass.Email).FirstOrDefault()
    

    Fernando

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Kozzer Sunday, April 24, 2011 5:42 PM
    Sunday, April 24, 2011 5:24 PM

All replies

  • Hi Kozzer;

    The query will return a collection therefore you need to instruct it to return one. The query below will return the first in the collection.

    Dim realPass = (From pass In BeanBagDataSet.Users _
            Where pass.Username = LogIn.currentUser _
            Select pass.Email).FirstOrDefault()
    

    Fernando

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, April 24, 2011 3:18 PM
  • Thanks for the response Fernando!

    One more question. I want to return the value of a single column, if the query returns a single row how do I get the value of a specific column?

    I know these are very basic question, I'm just beginning to learn VB.net. Thanks!

    Sunday, April 24, 2011 4:56 PM
  • Hi Kozzer;

    In the below query BeanBagDataSet.Users represents the collection of rows in the Users table and for every iteration of that collection pass represents a single row of the collection. In the Where clause you compare the column Username in the row with a variable LogIn.currentUser. In the Select clause is where you select what you want to return. If you return pass as in, Select pass, you are retuning the complete row. If you just want a single column returned then you select that one column, for example, Select pass.Email, which states to select from the current row the column Email. By add the FirstOrDefault() method, ( query body ).FirstOrDefault(), you are selecting the first item in the collection of the return set if one exist. If there are no values in the collection a default value is returned. That value depends on the data type of the returned value, for example in this case you are returning a string value of Email so if no values are in the return set a String.Empty is returned. If the returned value were of a reference type then a null value would be returned.

    Dim realPass = (From pass In BeanBagDataSet.Users _
        Where pass.Username = LogIn.currentUser _
        Select pass.Email).FirstOrDefault()
    

    Fernando

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Kozzer Sunday, April 24, 2011 5:42 PM
    Sunday, April 24, 2011 5:24 PM