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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With