Reputation: 10206
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
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
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
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