How can I translate this SQL statement to a Linq-to-SQL approach?
For example, imagine that I want to see if a user exists in my database:
Select * from Users where inputID = Users.ID
Then if that result brought > 0 items, then the user exists, correct?
How can I do something like this using a pure开发者_开发问答 Linq-to-SQL class?
dbContext.Users.Any(x => x.ID == inputID)
var user = dbContext.GetTable<User>().SingleOrDefault(u => u.ID == inputID);
bool userExists = user != null;
That will fetch the matching user from the database, if you just want to check for existance you can do this:
int matchingUsers = dbContext.GetTable<User>().Count(u => u.ID == inputID);
bool userExists = matchingUsers > 0;
or
bool userExists = dbContext.GetTable<User>().Any(u => u.ID == inputID);
精彩评论