views:

113

answers:

3

I am using the following

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

In this now i want to know, is there any result returned by this query? How to do this.

+6  A: 

This should work:

if (validLogin.Count() > 0) 
{
     //do work
}
The Matt
A: 

if (validLogin.Count() > 0){}

Chen Kinnrot
+9  A: 

Don't use Count() - use Any() unless you actually care about the count.

You can actually simplify this a lot, because you don't use the rest of the results either:

bool validLogin = DataContext.Persons.Any(p => p.UserName == login &&
                                               p.Password == password);

The nice thing about Any() is that whatever's processing the query can stop as soon as it's found any matching results - it doesn't need to keep looking for other potential matches. (Count() will work of course, it's just not as efficient, mostly because the operator itself isn't describing what you really care about as accurately.)

Jon Skeet