Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with SQL statement for count(*)

I am using SQL Data Server created in Visual Studio 2010.I want to see number of users with some username.

var x = from u in db.UserInfoes
                        where
                        u.Password == password &&
                        u.Username == username
                        select count(*);

My problem is count(*).VS doesnt accept this.Is there a way to write this?

like image 989
daidai Avatar asked Jul 20 '26 18:07

daidai


1 Answers

You could also do it this way:

var x = db.UserInfoes
    .Count(u => u.Password == password && u.Username == username);

The where clause can be seen of as redundant, as you can put your filter(the predicate) right inside the Count() method

But since you are checking for a SINGLE record (assuming only one user matches a username and password combo) You actually want to use the ANY method and get a Boolean

var x = db.UserInfoes
    .Any(u => u.Password == password && u.Username == username);

Any will be slightly faster than count, as it will return true as soon as it finds a match, instead of going through the entire table to ensure an exact count.

like image 64
Neil N Avatar answered Jul 22 '26 09:07

Neil N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!