简体   繁体   English

如何将此SQL语句转换为Linq-to-SQL方法?

[英]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? 然后,如果该结果带来了> 0个项目,那么该用户存在,对吗?

How can I do something like this using a pure Linq-to-SQL class? 如何使用纯Linq-to-SQL类执行类似的操作?

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM