Waheed
Waheed

Reputation: 10206

Getting value from VAR

For the following code

var validate = from P in this.DataContext.Persons
                         where P.UserName.Equals(login) && P.Password.Equals(password)
                         select new
                         {
                             P.FirstName,
                             P.LastName,
                             P.EmailAddress
                         };

If record exists i want to get the first name and return it. How can i get the firstName from var validate?

Upvotes: 2

Views: 5152

Answers (3)

Igal Tabachnik
Igal Tabachnik

Reputation: 31548

Try the following:

var validate = (from P in this.DataContext.Persons
                where P.UserName.Equals(login) && P.Password.Equals(password)
                select new
                {
                  P.FirstName,
                  P.LastName,
                  P.EmailAddress
                }).FirstOrDefault();

if (validate != null) 
{ 
  var firstName = validate.FirstName;
  ...
}

Upvotes: 0

Ralph Shillington
Ralph Shillington

Reputation: 21098

Console.WriteLine(validate.FirstOrDefault().FirstName);

Otherwise you'll have to loop through the set since that what your query is returning Likely a set of one but it's still a set.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062820

validate here is going to be a set (IQueryable<T>) of data. You may need to use FirstOrDefault(), for example:

var record = validate.FirstOrDefault();
if(record != null) {
    string firstName = record.FirstName;
}

Upvotes: 4

Related Questions