【发布时间】:2014-04-23 04:32:03
【问题描述】:
我正在构建一个 MVC 应用程序并希望翻译以下简单的 SQL 查询
SELECT logon_count from application_users where username = 'username_to_find'
我是 LINQ 和实体框架的新手。 我需要将 logon_count 结果存储为 C# int 并且只期望一个结果。为了安全起见,可能应该包含 1 的限制。
以下类是使用 ado.net 从数据库中自动创建的,代表 application_users 表。
public partial class application_users
{
public int idapplication_users { get; set; }
public string username { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public Nullable<int> logon_count { get; set; }
}
我也有 dbcontext
private thedbcontext dbcontext= new thedbcontext (); //In the controller
public partial class mycontext : thedbcontext
{
public virtual DbSet<application_users> application_users { get; set; }
}
有人可以建议我如何更好地利用 LINQ/Entity Framework 来执行上述 SQL 并获得 c# 整数形式的结果。
【问题讨论】:
标签: c# sql linq entity-framework-5 asp.net-mvc-5