views:

54

answers:

3

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?

+2  A: 

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;
}
Marc Gravell
+1  A: 
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.

Ralph Shillington
that could go very wrong in the (quite likely) case that there is no match...
Marc Gravell
A: 

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;
  ...
}
hmemcpy